简体   繁体   English

React Redux:从容器到组件传递道具和分派的问题

[英]React Redux : Problem with passing props and dispatch from container to component

I have a simple WORKING React-redux application which I tried to better organize into different folders like this:我有一个简单的 WORKING React-redux 应用程序,我试图将其更好地组织到不同的文件夹中,如下所示:
在此处输入图片说明

component1.js组件1.js

import React from "react";
class Presentational extends React.Component {
    constructor (props){
      super(props);
      this.state= {
        input:''
      }
      this.handleChange= this.handleChange.bind(this)
      this.submitMessage= this.submitMessage.bind(this)
    }
    handleChange (event) {
      this.setState ({
        input:event.target.value
      })
    }

    submitMessage () {
      this.props.submitNewMessage(this.state.input);
      this.setState({
        input:''
      })
    }

    render(){
      return (
        <div>
          <h2> Type a new Message </h2>
          <input value={this.state.input} onChange={this.handleChange}/>
          <br/>
          <button onClick={this.submitMessage}>Submit</button>
          <ul>
          {this.props.messages.map((message,idx) =>{
            return <li key={idx}>{message}</li>
          })}
          </ul>
        </div>
      )
    }
  }

export default Presentational;

container1.js容器1.js

import { connect } from 'react-redux'
import { addMessage } from '../actions'
import Presentational from '../components'

const mapStateToProps = state => {
    return {
      messages: state
    }
  }
  const mapDispatchToProps = dispatch => {
    return {
      submitNewMessage :message => {
        dispatch(addMessage(message))
      }
    }
  }

  export default connect(mapStateToProps,mapDispatchToProps)(Presentational)

While the app works in the original app where all of the react-redux code is in 1 file, after separating the logic of the file into different folders, the app crashes and gives these errors:虽然应用程序在原始应用程序中运行,其中所有 react-redux 代码都在 1 个文件中,但在将文件的逻辑分离到不同的文件夹后,应用程序崩溃并出现以下错误:

TypeError: Cannot read property 'map' of undefined类型错误:无法读取未定义的属性“地图”

Uncaught TypeError: this.props.submitNewMessage is not a function未捕获的类型错误:this.props.submitNewMessage 不是函数

It seems to me that the container1.js code isn't successfully 'mapping state to props' and 'mapping dispatch to props' for the Presentational component in component1.js.在我看来,container1.js 代码没有成功地为 component1.js 中的 Presentational 组件“将状态映射到道具”和“将分派映射到道具”。

Any idea what I did wrong?知道我做错了什么吗?

You have to use the container1.js file instead of the component1.js file, because the component1 doesn't have the Redux props, but the container1 does.您必须使用container1.js文件而不是component1.js文件,因为 component1 没有 Redux props,但 container1 有。

So, you should either change your imports to use the container1 file or put the Redux prop mapping into the component1.js file因此,您应该更改导入以使用 container1 文件或将 Redux prop 映射放入component1.js文件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM