I'm trying to figure out why props (mapped from the redux store) aren't updated after calling dispatch
. For example, in the code below dispatch the RANDO action, which sets a random number. Immediately after dispatching if I access this.props.rando
it doesn't contain the updated value. Is this expected behavior, or am I doing something wrong?
// reducer.js
module.exports = (state = {}, action) => {
const newRando = Math.round(Math.random() * 100)
console.log("Reducer: rando = ", newRando);
return { rando: newRando }
}
// app.jsx
const { connect } = require('react-redux')
const React = require('react')
class App extends React.Component {
onClick() {
console.log("Clicked: rando = ", this.props.rando)
this.props.dispatch({ type: 'RANDO' })
console.log("After Dispatch: rando = ", this.props.rando)
setTimeout(() => console.log("After setTimeout: rando = ", this.props.rando), 1)
}
render() {
return (
<div>
<div onClick={() => this.onClick()}>HI</div>
</div>)
}
}
App.propTypes = {
dispatch: React.PropTypes.func.isRequired,
rando: React.PropTypes.number.isRequired,
}
const mapStateToProps = state => {
return ({
rando: state.rando
})
}
module.exports = connect(mapStateToProps)(App)
The output after I click the HI
div is
Clicked: rando = 36
Reducer: rando = 48
After Dispatch: rando = 36
After setTimeout: rando = 48