score:2

Accepted answer

mapdispatchtoprops provides you a way to connect your action creators to your components. let's assume you have an action creator which increments a counter state

export const change = value =>({
    type: 'change_fruit',
    fruit : value
})

and you want value to be passed from one of your components. first use connect hoc in this component like you're already doing. now you need to import incrementcounter from your actions folder

import { change as changefruit } from './actions/fruit'

now use mapdispatchtoprops like this

const mapdispatchtoprops = dispatch =>({
    change : fruit => dispatch(changefruit(fruit))
})

now you have an action creator serialized inside your component's props and when you call props.increment(2) this will be the equivalent to call

dispatch(changefruit('apple'))

here is why you should always do props.increment instead of directly call dispatch inside your component.

so the full implementation inside your component could be like this

import { change as changefruit } from './actions/fruit'

class component extends react.component{
    render(){
        const { fruit, change } = this.props
        return(
            <>
                <div>{fruit}</div>
                <button onclick={() => change('orange')}>change to orange</button>
            </>
        )
    }
}

const mapstatetoprops = state =>({
    fruit : state.fruit
})

const mapdispatchtoprops = dispatch =>({
    change : fruit => dispatch(changefruit(fruit))
})

export default connect(mapstatetoprops, mapdispatchtoprops)(component)

your reducer should look like this

const initialstate = {
    fruit: 'apple'
}

export const reducer = (state = initialstate, action) =>{
    switch(action.type){
        case 'change_fruit' : return{
            ...state,
            fruit : action.fruit
        }

        default : return state
    }
}

score:0

an "action" is just a plain object that describes some sort of change you want to happen in your store. so in your case you want to add text to your string, such an action might look like:

{
  type: 'add_text',
  value: 'red delicious'
}

an action creator is nothing more than a function that returns such a plain object. notice we can generalise the action creator, so you can pass in the string to add, rather than have it hard coded as 'red delicious'.

const addtext = value => ({ type: 'add_text', value })

in order to 'send' this action to the reducers, it needs to be passed to dispatch function that redux provides. e.g. this would dispatch the action:

const action = addtext('red delicious') // create the plain action object
dispatch(action) // send it to the reducers

that can get kind of tedious to write out manually all the time, so mapdispatchtoprops helps with that. it's a function you provide to connect that does the above all in one place, leaving the rest of your component code un-cluttered. redux calls it internally to generate the props for your connected component. so when you do,

const mapdispatchtoprops = dispatch => ({
  addtext: value => {
    // same code as above:
    const action = addtext('red delicious')
    dispatch(action)
  }
})

in your component you can call

props.addtext('red delicious')

score:0

look when use the connect function in the app it takes 2 arguments the first is the state current and the second argument is the dispatch that specifies what type of action that will be implemented is the specific action the dispatch depended on the despatch will be called the specific action that link by reducer that implements in provider in-store when use

<provider store={store}>

which the reducer that created by the const store=createstore(rootreducer)

    const mapdispatchtoprops = (dispatch) => {
    return{
      example look this is dispatch that well happened it will call specif action bu use key  type 
this == store.dispatch(constants.widithroup)
the first is key or name of the handle when you want to implement :(input that enters fro this dispatch and=> it call store.dispatch(name of handel) )
      widthroup: (balance) => dispatch({ type: constants.widithroup ,balance}),
      deposit: (balance) => dispatch({ type: constants.deposit,balance })    }
}
}

score:0

basically i am answering for the part where you have confusion:

return {
    what goes in here?
}

ideally, if you think as class concept, let's say you have a class that facilitates to choose and save your favorite color selection.

so, all you want is to create a class component and define a method saveselection that should save selection to store. and, who ever wants to get that selection or see your selected colors they can access from store.

back to redux, the rule says, you can not update the state of store directly. you have to dispatch an action.

so, if i already have a decided to create a method saveselection for that purpose, what should i do, so that it should dispatch an action.

may be something like:

class colorselector {
    saveselection () {
      dispatch(action); //dispatch is some way to dispatch an action
    }
}

but, action is not just a command or text or a value, we need to specify, the type of action (because reducer need to identify which data it need to update based on action) and the data i want to save. that will make sense of an action.

so, i want something like:

 //i pass my selected color to saveselection method and that should save
 saveselection(datatoupdate) {
      dispatch({type: actiontype, data: datatoupdate})
 }

one thing may be going inside your head -> how will i get dispatch and how did i just use it here? that will get clear in a sec, but first lets replace the dispatch arguments which we can write like:

saveselectionaction(datatoupdate) {
  return {type: actiontype, data: datatoupdate}
}

and our method would be like:

saveselection(datatoupdate) {
  dispatch(saveselectionaction(datatoupdate))
}

and saveselectionaction becomes the action creator part. now comes the dispatch part. so, lets wrap this method with another method which provides dispatch and expose saveselection

anothermethodwhichpassesdispatch(dispatch) {
    // unleash the power of closure
    return {
        saveselection: saveselection(datatoupdate) {
            dispatch(saveselectionaction(datatoupdate))
        }
    }
}

or more correctly

// method to map dispatching action to components props
anothermethodwhichpassesdispatch(dispatch) {
    // return list of your created action you want as props
    return {


        // name it whatever you want and that will be a void method passing data
        saveselection: (datatoupdate) => {
            // pass the action creator's result which is defined above
            // or, exported from a separate file
            dispatch(saveselectionaction(datatoupdate))
        }
    }
}

now pass this method as argument to connect hoc and that will map saveselection to a props and provide dispatch to your method. connect(mapstatetoprops, anothermethodwhichpassesdispatch)(<container>) or we can rename it as mapdispatchtoprops.

now go back to your class's saveselection method and do like:

saveselection = (selectedcolor) => {
    this.props.saveselection(selectedcolor);
}

that's it.


Related Query

More Query from same tag