0

I am new to redux with react.I don't know why below code contains 2 arrow function and the purpose of it.

export const loadName = () => (dispatch: any) => {
    const name = localStorage.getItem('name'); 
    dispatch(setName(name));
}
2
  • I think loadName is function without parameter which returns a function which accepts dispatch as parameter and its body is { const name = localStorage.getItem('name'); dispatch(setName(name)); } Commented Aug 2, 2017 at 7:34
  • Possible duplicate of Syntax of arrow functions Commented Aug 2, 2017 at 8:50

1 Answer 1

2

Basically it's a function returning another function, equivalent to:

export const loadName = () => {
  return (dispatch: any) => {
    const name = localStorage.getItem('name'); 
    dispatch(setName(name));
  };
};
Sign up to request clarification or add additional context in comments.

2 Comments

We can do without arrow function within arrow function means with simple one arrow function. But why we use this.
No real reason since it's not using this I think -- it's just shorter to use an arrow instead of writing function, and allows you to write it in one line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.