0

this is my action, I straight away dispatch an object in getMsgList

export function getMsgList(){
    dispatch=>{
        axios.get('/user/getmsglist')
        .then(res=>{
            if(res.state===200 && res.data.code===0){
                dispatch({type:MSG_LIST, payload:res.data.data})
            }
        })
    }
}

but I've seen some people create a function instead, they'll do it like this.

...
dispatch(getMsg(res.data.data)
...

getMsg(msg){
    return{type:MSG_LIST,payload:msg}
}

What is the purpose of that 'extra function'?

Jenny Mok
  • 2,744
  • 9
  • 27
  • 58
  • I recommend you to read some posts on this blog. https://daveceddia.com/ This person is really good at explaning on things in React – moon Dec 05 '17 at 05:17
  • Possible duplicate of [Javascript: Inline function vs predefined functions](https://stackoverflow.com/questions/2539205/javascript-inline-function-vs-predefined-functions) – Vipin Kumar Dec 05 '17 at 05:24

1 Answers1

0

[Redux] (https://redux.js.org/docs/basics/Actions.html) explains this. This is called an Action Creator.

In a real app, it is wiser to generate a unique ID every time something new is created. It's a good idea to pass as little data in each action as possible. For example, it's better to pass index than the whole todo object. Action creators are exactly that—functions that create actions.

Essentially it reduces copy and paste for the type string, save code, and clarify logic using an Action Creator

mckuok
  • 744
  • 6
  • 11