Javascript 我可以在 Redux 中没有 mapStateToProps 的情况下 mapDispatchToProps 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47657365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:54:12  来源:igfitidea点击:

Can I mapDispatchToProps without mapStateToProps in Redux?

javascriptreactjsreduxreact-redux

提问by Iggy

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

我正在分解 Redux 的 todo 示例以试图理解它。我读到mapDispatchToProps允许您将调度操作映射为道具,因此我想到重写addTodo.js以使用 mapDispatchToProps 而不是调用 dispatch(addTodo())。我叫它addingTodo()。像这样的东西:

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 mapStateToPropsto begin with on AddTodo component, so I was not sure what was wrong. My gut feeling says that connect()expects mapStateToPropsto precede mapDispatchToProps.

但是,当我运行该应用程序时,出现以下错误:Error: Invalid value of type object for mapStateToProps argument when connecting component AddTodo.. 我从来没有mapStateToProps开始使用 AddTodo 组件,所以我不确定出了什么问题。我的直觉说,connect()期待mapStateToProps领先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?

所以我的问题是,是否可以在没有 mapStateToProps 的情况下执行 mapDispatchToProps?我正在尝试做的是一种可接受的做法 - 如果不是,为什么不呢?

回答by iofjuupasli

Yes, you can. Just pass nullas first argument:

是的你可以。只需null作为第一个参数传递:

AddTodo = connect(
    null,
    mapDispatchToProps
)(AddTodo)

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

是的,这不仅是可接受的做法,而且是触发操作的推荐方式。使用mapDispatchToProps允许隐藏在 React 组件中使用 redux 的事实