If you want to use mapDispatchToProps
without a mapStateToProps
just use null
for the first argument.
export default connect(null, mapDispatchToProps)(Start)
You are just missing the first argument to connect
, which is the mapStateToProps
method. Excerpt from the Redux todo app:
const mapStateToProps = (state) => {
return {
todos: getVisibleTodos(state.todos, state.visibilityFilter)
}
}
const mapDispatchToProps = (dispatch) => {
return {
onTodoClick: (id) => {
dispatch(toggleTodo(id))
}
}
}
const VisibleTodoList = connect(
mapStateToProps,
mapDispatchToProps
)(TodoList)
Use
const StartContainer = connect(null, mapDispatchToProps)(Start)
instead of
const StartContainer = connect(mapDispatchToProps)(Start)
I solved it by interchanging the arguments, I was using
export default connect(mapDispatchToProps, mapStateToProps)(Checkbox)
which is wrong. The mapStateToProps
has to be the first argument:
export default connect(mapStateToProps, mapDispatchToProps)(Checkbox)
It sounds obvious now, but might help someone.
I needed an example using React.Component
so I am posting it:
import React from 'react';
import * as Redux from 'react-redux';
class NavigationHeader extends React.Component {
}
const mapStateToProps = function (store) {
console.log(`mapStateToProps ${store}`);
return {
navigation: store.navigation
};
};
export default Redux.connect(mapStateToProps)(NavigationHeader);
Here are a couple of things to notice in order to understand the connected component's behavior in your code:
The Arity of connect
Matters: connect(mapStateToProps, mapDispatchToProps)
React-Redux calls connect
with the first argument mapStateToProps
, and second argument mapDispatchToProps
.
Therefore, although you've passed in your mapDispatchToProps
, React-Redux in fact treats that as mapState
because it is the first argument. You still get the injected onSubmit
function in your component because the return of mapState
is merged into your component's props. But that is not how mapDispatch
is supposed to be injected.
You may use mapDispatch
without defining mapState
. Pass in null
in place of mapState
and your component will not subject to store changes.
Connected Component Receives dispatch
by Default, When No mapDispatch
Is Provided
Also, your component receives dispatch
because it received null
for its second position for mapDispatch
. If you properly pass in mapDispatch
, your component will not receive dispatch
.
The above answers why the component behaved that way. Although, it is common practice that you simply pass in your action creator using mapStateToProps
's object shorthand. And call that within your component's onSubmit
That is:
import { setAddresses } from '../actions.js'
const Start = (props) => {
// ... omitted
return <div>
{/** omitted */}
<FlatButton
label='Does Not Work'
onClick={this.props.setAddresses({
pickup: this.refs.pickup.state.address,
dropoff: this.refs.dropoff.state.address
})}
/>
</div>
};
const mapStateToProps = { setAddresses };
export default connect(null, mapDispatchToProps)(Start)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With