简体   繁体   English

React / Redux-将道具从一个容器传递到另一个

[英]React/Redux - Passing props from one container to another

I'm fairly new to Redux so if my implementation is wrong, please advice. 我是Redux的新手,所以如果我的实现错误,请提出建议。

In my current app, I have two containers. 在我当前的应用中,我有两个容器。 Essentially the information that I want is: from one of the container, I'd like to pass a prop to the other container. 本质上,我想要的信息是:从一个容器中,我想将一个道具传递给另一个容器。

Specifically, I want myArray variable from Board to pass it down to the Controller container. 具体来说,我希望Board的 myArray变量将其向下传递到Controller容器。

Here's what I've done so far: 到目前为止,这是我所做的:

containers/board 容器/板

class Board extends Component {
  constructor(props){
    super(props);
    this.onClickToggle = this.onClickToggle.bind(this)
  }
  onClickToggle(coord){
    this.props.onCellClick(coord)
  }
  componentDidMount() {

  }
  render(){
    const height = this.props.grid.height
    const width = this.props.grid.width
    let rows = [];
    let myArray = []
    for (let y = 0; y < height; y++) {
      let rowID = `row${y}`;
      let bin = [];
      let obj = {}
      for (let x = 0; x < width; x++){
        let cellID = `${y}-${x}`;
        let index = y * width + x //index of grid;
        let status = this.props.grid.cells[index];//0 = dead, 1 = alive
        bin.push(
          <Cell
            key={x}
            id={cellID}
            status={status}
            onClick={() => this.onClickToggle({x,y})}
          />)
        obj[cellID] = status
      }
      rows.push(<tr key={y} id={rowID}>{bin}</tr>)
      myArray.push(obj);
      <Controller coord={myArray} />
    }
    return(
      <div className="container">
        <div className="row">
          <div className="col s12 board"></div>
            <table id="simple-board">
               <tbody>
                 {rows}
               </tbody>
             </table>
          </div>
      </div>
    )
  }
}

function mapDispatchToProps(dispatch){
  return bindActionCreators({onCellClick}, dispatch)
}
function mapStateToProps(state) {
  return {
    grid: state.grid
  };
}

export default connect(mapStateToProps,mapDispatchToProps)(Board)

containers/controller 容器/控制器

class Controller extends Component {
  constructor(props){
    super(props);
    this.test = this.test.bind(this);
  }
  componentDidMount() {
  }

  test(){
    // this.props.start
    console.log(this.props)
  }
  render(){
    return(
      <div className="container">
        <div className="row">
          <div className="col s12 controller">
            <a onClick={this.test} className="waves-effect waves-light btn">Start</a>
            <a onClick={this.props.clear} className="waves-effect waves-light btn">Clear</a>
            <a onClick={this.props.randomize}className="waves-effect waves-light btn">Randomize</a>
          </div>
        </div>
      </div>
    )
  }
}

function mapDispatchToProps(dispatch){
  return bindActionCreators({clear,randomize,start}, dispatch)
}


export default connect(null,mapDispatchToProps)(Controller)

I understand how to pass props to components, but I haven't faced a situation of passing props from one container to another one. 我知道如何将道具传递到组件,但是我还没有遇到过将道具从一个容器传递到另一个容器的情况。

Remove <Controller coord={myArray} /> from the for loop. 从for循环中删除<Controller coord={myArray} /> You can nest the controller inside return statement like below, to access its values. 您可以将控制器嵌套在return语句中,如下所示,以访问其值。

containers/board 容器/板

return(

  <div className="container">

   <Controller coord={myArray} />

    <div className="row">
      <div className="col s12 board"></div>
        <table id="simple-board">
           <tbody>

           </tbody>
         </table>
      </div>
  </div>
)

containers/controller 容器/控制器

test(){
  // this.props.start
  console.log(this.props.coord)
}

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

相关问题 React Redux:从容器到组件传递道具和分派的问题 - React Redux : Problem with passing props and dispatch from container to component 将 React 中的道具从一个文件传递到另一个文件 - Passing props in React from one file to another 获取TypeError:_red2.props.handleClick不是一个函数,当将redux prop从容器传递到react中的表示组件时 - Getting TypeError: _this2.props.handleClick is not a function when passing redux prop from container to presentational component in react 反应,将未定义的道具从一个组件路由到另一个组件? - React, route passing props undefined from one component to another? 从 Redux 传递道具 - Passing props from Redux React Redux - 通过动作传递道具 - React Redux - passing props with actions 使用typescript在将prop从一个组件传递到另一个组件时发生错误,反之亦然 - Error Coming when passing props from one component to another and vice - versa in react using typescript React - 如何通过将状态作为道具传递,基于从另一个下拉列表中的选择来填充一个下拉列表 - React - How to Populate one Dropdowns based on selection from another Dropdown by Passing State as props 使用 fetch 将 Container 中的 props 通过 Link 传递给一个元素 - Passing props from Container with fetch to one element via Link 将 props 传递给另一个文件 redux 中的组件 - Passing props to component in another file redux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM