sbalay / react-native-redux-toast

Simple to use, easy to customize Toast component for Android & iOS. Developed with love for redux apps
50 stars 23 forks source link

this.props.dispatch is not a function. #6

Open PManager1 opened 6 years ago

PManager1 commented 6 years ago

image I was tryign to use this in the component but i got this error message, how do i fix it ?

I think th eissue is with the store coz Im using the redux-thunk, How can i fix it ?

Heres mycode :

import { createStore, compose, applyMiddleware } from 'redux'; import thunk from 'redux-thunk'; import reducers from '../reducers';

const store = createStore( reducers, {}, compose ( applyMiddleware(thunk) ) ); export default store;

//And my reducer code, is.

import { combineReducers } from 'redux'; import auth from './auth_reducer'; import jobs from './jobs_reducer';

// New import LibraryReducer from './comps/LibraryReducer' import SelectionReducer from './comps/SelectionReducer' import { toastReducer as toast } from 'react-native-redux-toast';

export default combineReducers({ auth: auth, jobs: jobs, form: form, libraries: LibraryReducer, selectedLibraryId: SelectionReducer,

toast, });

//

fahrurben commented 6 years ago

` import { createStore, applyMiddleware } from 'redux'; import thunk from 'redux-thunk'; import rootReducer from './rootReducer';

export default function configureStore(initialState) { return createStore( rootReducer, initialState, applyMiddleware(thunk) ); } `

This is the format of createStore i create, i believe it's no need to put the component (toast) into createStore

PManager1 commented 6 years ago

@fahrurben hows your code different than what i have above ?

Also the issue that i m facing is due to this,,

I am stuck here, coz I want to use this toaster library via redux

but when i do that I get the error message : this.props.dispatch is not a function. bla bla dispatch undefined.

New code after usual componentwillmount() and navigation bla bla bla,

pastedgraphic-1 pastedgraphic-2

Now this works fine

pastedgraphic-3

But if i do need the actions too. the moment i add the following lines, pastedgraphic-4

pastedgraphic-5

It complains and you know why that complains,

pastedgraphic-6

How to “ use the shortcut by passing an object directly to 'connect’. “ ?

PManager1 commented 6 years ago

@fahrurben if i do as you suggested, i get the following error. Moreover i read a number of places that all the reducer related code shoudl be inside the redudcers and not in the store. Any other suggestions after looking into my code ?

My major concern is i cant call the Actions from with in the class componet. image

fahrurben commented 6 years ago

From the first i look, the problem are with reducers. Do you have combine the reducers with 'toast' Reducer ? You can't put the two reducer in Create store, instead combine the reducer first.

fahrurben commented 6 years ago

It will need to check reducer, action, container, component to know the problem

PManager1 commented 6 years ago

@fahrurben your right about : "From the first i look, the problem are with reducers. Do you have combine the reducers with 'toast' Reducer ? You can't put the two reducer in Create store, instead combine the reducer first."

Ive provided you both the reduccer, action, component above.

fahrurben commented 6 years ago

Actually i don't see any action to map to the container. export default connect()(FollowupScreen); will enough, if not i will need to see what 'actions' you put to component.

PManager1 commented 6 years ago

Lol - yes it works fine if I don’t have any actions but the moment I try to use the actions it throws the errors.

Sent from my iPhone

On Sep 29, 2017, at 12:42 AM, Farhrurrazi notifications@github.com wrote:

Actually i don't see any action to map to the container. export default connect()(FollowupScreen); will enough, if not i will need to see what 'actions' you put to component.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

fahrurben commented 6 years ago

So, maybe this is maybe help you.

function mapDispatchToProps(dispatch) { return { dispatch, onClick: () => dispatch(increment()) }; }

Do you put the dispatch in returned object ? If not, maybe you can try it. I think this caused your issue.

https://stackoverflow.com/questions/34458261/how-to-get-simple-dispatch-from-this-props-using-connect-w-redux

PManager1 commented 6 years ago

I want to use redux, ive been using the state in every component.

PManager1 commented 6 years ago

function mapDispatchToProps(dispatch) { return { dispatch, onClick: () => dispatch(increment()) }; }

where is increment() coming from ?

PManager1 commented 6 years ago

and how do i wtite the last statement i.e export class ?

fahrurben commented 6 years ago

Change onclick with your action/function to map, if none so no need to define this

export default connect(
  null,
  mapDispatchToProps,
)(FollowupScreen));
PManager1 commented 6 years ago

this is what ive got , function mapDispatchToProps(dispatch) { return { dispatch, actions }; } export default connect( null, mapDispatchToProps )(FollowupScreen);

But it still cant access the Prioritiesfetch() in the componentWillMount

import * as actions from '../actions'; import { prioritiesFetch } from '../actions';

componentWillMount(){ this.props.prioritiesFetch(); }

PManager1 commented 6 years ago

it should be componentWillMount(){ this.props.actions.prioritiesFetch(); }

fahrurben commented 6 years ago

In container `import { prioritiesFetch } from '../actions';

const mapDispatchToProps = (dispatch) => { return { dispatch, prioritiesFetch: () => dispatch(prioritiesFetch()) } }; `

In your component `class FollowupScreen extends React.Component {

constructor(props) { super(props);

this.prioritiesFetch = props.prioritiesFetch;

}

componentWillMount(){ this.props.prioritiesFetch(); } }`

PManager1 commented 6 years ago

this is great,

constructor(props) { super(props);

this.prioritiesFetch = props.prioritiesFetch; }

But how woudl you suggest to implment in the case where we want to import all the actions with one go ?

ie import * as actions from '..actions';

PManager1 commented 6 years ago

@fahrurben so it worked for when i had only one action, I get error when i have more than 1 actions and ive to use the mapStateToProps, Here is my code, can you please explain what am I doing wrong const mapStateToProps = (state) => { console.log('203- UserProfileScreen - mapStateTo Props STATE=', state); console.log('204- UserProfileScreen - mapStateTo Props NEW-2 UserInfoForm ', state.UserInfoForm); const { _id, userId, user_state, email, phone_no, subscription_expire, gmailPassword, gmailId, subscription, signature_link } = state.UserInfoForm; return { _id, userId, user_state, email, phone_no, subscription_expire, gmailPassword, gmailId, subscription, signature_link }; // return { userinfo: null } };

const mapDispatchToProps = (dispatch) => { return { dispatch, userInfoFetch: () => dispatch(userInfoFetch()); userInfoSave: () => dispatch(userInfoSave()); userInfoUpdate: () => dispatch(userInfoUpdate()); } };

export default connect(mapStateToProps, mapDispatchToProps)(MyProfileScreen);

const mapStateToProps = (state) => { console.log('203- UserProfileScreen - mapStateTo Props STATE=', state); console.log('204- UserProfileScreen - mapStateTo Props NEW-2 UserInfoForm ', state.UserInfoForm); const { _id, userId, user_state, email, phone_no, subscription_expire, gmailPassword, gmailId, subscription, signature_link } = state.UserInfoForm; return { _id, userId, user_state, email, phone_no, subscription_expire, gmailPassword, gmailId, subscription, signature_link }; // return { userinfo: null } };

const mapDispatchToProps = (dispatch) => { return { dispatch, // userInfoFetch, // userInfoSave, // userInfoUpdate userInfoFetch: () => dispatch(userInfoFetch()); userInfoSave: () => dispatch(userInfoSave()); userInfoUpdate: () => dispatch(userInfoUpdate()); } };

export default connect(mapStateToProps, mapDispatchToProps)(MyProfileScreen);

sbalay commented 6 years ago

@jpca999 Can you upload an example project with your code to github? I'll probably will be more helpful taking a closer look the entire code

PManager1 commented 6 years ago

@sbalay hmm

fahrurben commented 6 years ago

If action need parameter, it must provide in mapDispatchToProps ` import { prioritiesFetch,userInfoSave } from '../actions';

const mapDispatchToProps = (dispatch) => { return { dispatch, prioritiesFetch: () => dispatch(prioritiesFetch()), userInfoSave: (customer) => dispatch(userInfoSave(customer)); } };`