Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I mapDispatchToProps without mapStateToProps in Redux?

I am breaking apart Redux' todo example to try to understand it. I read that mapDispatchToProps allows you to map dispatch actions as props, so I thought of rewriting addTodo.js to use mapDispatchToProps instead of calling dispatch(addTodo()). I called it addingTodo(). Something like this:

import React from 'react'; import {connect} from 'react-redux'; import addTodo from '../actions';  let AddTodo = ({addingTodo}) => {   let input;   return (       <div>         <form onSubmit={e => {           e.preventDefault()           if (!input.value.trim()) {             return           }           addingTodo(input.value)           input.value = ""         }}>           <input ref={node => {             input = node           }} />           <button type="submit">Submit</button>         </form>       </div>   ) }  const mapDispatchToProps = {   addingTodo: addTodo }  AddTodo = connect(   mapDispatchToProps )(AddTodo)  export default AddTodo 

However, when I run the app, I get this error: Error: Invalid value of type object for mapStateToProps argument when connecting component AddTodo.. I never used mapStateToProps to begin with on AddTodo component, so I was not sure what was wrong. My gut feeling says that connect() expects mapStateToProps to precede mapDispatchToProps.

The working original looks like this:

import React from 'react'; import {connect} from 'react-redux'; import addTodo from '../actions';  let AddTodo = ({dispatch}) => {   let input;   return (       <div>         <form onSubmit={e => {           e.preventDefault()           if (!input.value.trim()) {             return           }           dispatch(addTodo(input.value))           input.value = ""         }}>           <input ref={node => {             input = node           }} />           <button type="submit">Submit</button>         </form>       </div>   ) }  AddTodo = connect()(AddTodo)  export default AddTodo 

Complete repo can be found here.

So my question is, is it possible to do mapDispatchToProps without mapStateToProps? Is what I am trying to do an acceptable practice - if not, why not?

like image 626
Iggy Avatar asked Dec 05 '17 15:12

Iggy


People also ask

Is it possible to dispatch an action without using mapDispatchToProps?

Without mapDispatchToPropsNotice that the component receives a dispatch prop, which comes from connect() , and then has to use it directly to trigger the actions.

Why we use mapStateToProps in Redux?

As the first argument passed in to connect , mapStateToProps is used for selecting the part of the data from the store that the connected component needs. It's frequently referred to as just mapState for short. It is called every time the store state changes.

What do mapStateToProps and mapDispatchToProps actually do?

The mapStateToProps and mapDispatchToProps deals with your Redux store's state and dispatch , respectively. state and dispatch will be supplied to your mapStateToProps or mapDispatchToProps functions as the first argument.


1 Answers

Yes, you can. Just pass null as first argument:

AddTodo = connect(     null,     mapDispatchToProps )(AddTodo) 

Yes, it's not just acceptable practice, it's recommended way to trigger actions. Using mapDispatchToProps allows to hide the fact of using redux inside your react components

like image 77
iofjuupasli Avatar answered Oct 14 '22 15:10

iofjuupasli