Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get dispatch redux

I'm learning redux and react. I am following some tutorials, in order to make a app.

I have this action:

export function getDueDates(){
  return {
    type: 'getDueDate',
    todo
  }
}

this is the store:

import { createStore } from 'redux';
import duedates from './reducers/duedates'

export default createStore(duedates)

This is the reducer:

import Immutable from 'immutable'

export default (state = Immutable.List(['Code More!']), action) => {
  switch(action.type) {
    case 'getDueDate':
      return state.unshift(action.todo)
    default:
      return state
  }
}

and in the entry point js I have this:

import React from 'react';
import ReactDOM from 'react-dom';

import store from './app/store'
import { Provider } from 'react-redux'

import App from './app/Components/AppComponent';


ReactDOM.render(
    <Provider store={store}>
        <App />
    </Provider>,
    document.getElementById('app')
);

Now, (according to some examples), I should call getDueDate from the dispatch but I dont get how to get the dispatch on the component, to trigger the action

like image 255
Pablo Avatar asked Feb 20 '16 17:02

Pablo


People also ask

How do I access dispatch Redux?

Nevertheless, when you want to dispatch an action from your component, you should first connect it with the store and use the connect method of react-redux (2nd way). Then, when you start to have logic in your mapDispatchToProps function, it's time to dispatch action in your saga (3rd way).

What is Dispatch in Redux?

Dispatch​ The Redux store has a method called dispatch . The only way to update the state is to call store.dispatch() and pass in an action object. The store will run its reducer function and save the new state value inside, and we can call getState() to retrieve the updated value: store.

How do I use dispatch in Redux saga?

Create a plain JavaScript Object to instruct the middleware that we need to dispatch some action, and let the middleware perform the real dispatch. This way we can test the Generator's dispatch in the same way: by inspecting the yielded Effect and making sure it contains the correct instructions.

How do I dispatch action in reducer?

Dispatching an action within a reducer is an anti-pattern. Your reducer should be without side effects, simply digesting the action payload and returning a new state object. Adding listeners and dispatching actions within the reducer can lead to chained actions and other side effects.


2 Answers

Use connect from react-redux package. It has two functions as params, mapStateToProps and mapDispatchToProps, which you are interested in now. As per answer from Nick Ball, which is partially right, you will be exporting like this:

export default connect(mapStateToProps, mapDispatchToProps)(App)

and your mapDispatchToProps will look something like this:

function mapDispatchToProps (dispatch, ownProps) {
  return {
    getDueDate: dispatch(getDueDate(ownProps.id))
  }
}

as long as your component connected to the store has property id passed from above, you'll be able to call this.props.getDueDate() from inside of it.

EDIT: There is probably no need of using an id in this case, however my point was to point out that props go as second parameter :)

like image 50
george.cz Avatar answered Nov 03 '22 00:11

george.cz


The missing piece here is the connect function from react-redux. This function will "connect" your component to the store, giving it the dispatch method. There are variations on how exactly to do this, so I suggest reading the documentation, but a simple way would be something like this:

// app/Components/AppComponent.js

import { connect } from 'react-redux';

export class App extends React.Component {

    /* ...you regular class stuff */

    render() {

        // todos are available as props here from the `mapStateToProps`
        const { todos, dispatch } = this.props;

        return <div> /* ... */ </div>;
    }
}

function mapStateToProps(state) {
    return {
        todos: state.todos
    };
}

// The default export is now the "connected" component
// You'll be provided the dispatch method as a prop
export default connect(mapStateToProps)(App);
like image 24
Nick Ball Avatar answered Nov 02 '22 23:11

Nick Ball