abu abu
abu abu

Reputation: 7028

Pass values from a component to Redux Store

I am learning Redux with React.I have a Form in a component.I would like to pass data from that component to actions of Redux. I am using below code for submitting that Form.

handleSubmit = event => {
    var mouse = 'mouse';
    this.props.dispatch(mouse);
  }

My action is like below

const authActions = (value) => {
    return {
        type: 'Hello',
        payload: value
    };
}

export default authActions;

My Reducer is like below

 import authActions from '../actions/authActions';

    const initialState = {
        user: {},
        error: {}
    }

    const reducer = (state = initialState, action) => {
        switch (action.type) {
            case 'Hello': {
                console.log(action.payload);
            }
            default: return state;
        }
    }

    export default reducer;

May be I could not express myself properly. I don't know whether I am in a right way of learning Redux or not. Please help me anyone.

How can I send data from my component to action ?

Upvotes: 0

Views: 223

Answers (1)

lecstor
lecstor

Reputation: 5707

Ideally you'd be using mapActionsToProps wrap authActions in dispatch and add it to your props, but if you have the redux dispatch method in your props as you appear to here you should be able to simply do

handleSubmit = event => {
  var mouse = 'mouse';
  this.props.dispatch(authActions(mouse));
}

authActions is an action creator. You call it with your value and get an action back. You then dispatch the action to the redux store.

Upvotes: 1

Related Questions