M
M
mr jeery2018-02-02 15:49:07
JavaScript
mr jeery, 2018-02-02 15:49:07

How to create a field in state named action?

Good afternoon.
I'm new, I'm asking for help.
An action comes into my reducer with two values: action.name and action.payload.
I need to add a property to the state with the name action.name and the value action.payload

export const BuyReducer = (state = defaultState, action) => {
                return{...state,
                           state[action.name]: action:payload //неверно
                          }

}

For example action.name = "Dima", action.payload = 30, then state will be equal to
const defaultState = {
Dima: 30
}

How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Interface, 2018-02-02
@jeerjmin

const action = {name: 'Dima', payload: 30};
const state = {
    ...state,
    [action.name]: action.payload
};

Ps I would advise you to learn "vanilla" javascript before react / redux

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question