How to push data in object? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to push data in object? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m using Redux in my React App, and I need to put data to object (using action).

I have such reducer and action:

        case GET_USER_DATA: {
            return {
                ...state,
                user: action.value // user - is object
            }
        }

//my action
        export const getUserData = (value) => ({type: 'GET_USER_DATA',value})

And then I need to put data to my Redux state using action, but it not works, can anybody explaine me how to do this?

func:

    authListener() {
        fire.auth().onAuthStateChanged((user) => {
          console.log(user);
          if (user) {
            getUserData(user) // I need to put it here
            localStorage.setItem('user', user.uid);
          } else {
            getUserData(null)
            localStorage.removeItem('user');
          }
        });
      }

Answer

Have you added this function in mapDispatchToProps so that it can get the dispatch function and reducer can be called?

const mapStateToProps = (state: any) => {
    return {
        //any data from state
    };
};

function mapDispatchToProps(dispatch: any) {
    return bindActionCreators({
        getUserData
    }, dispatch);
}

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

You can now call the function as

props.getUserData(user);
 
We are here to answer your question about How to push data in object? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji