javascript - React/Redux - 将 Prop 从一个容器传递到另一个容器

标签 javascript reactjs containers react-redux

我对 Redux 还很陌生,所以如果我的实现有误,请提出建议。

在我当前的应用程序中,我有两个容器。本质上,我想要的信息是:从一个容器,我想将一个 Prop 传递给另一个容器。

具体来说,我希望 Board 中的 myArray 变量将其传递到 Controller 容器。

这是我到目前为止所做的事情:

容器/板

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)

容器/ 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)

我了解如何将 props 传递给组件,但我还没有遇到过将 props 从一个容器传递到另一个容器的情况。

最佳答案

删除<Controller coord={myArray} />来自 for 循环。您可以将 Controller 嵌套在 return 语句中,如下所示,以访问其值。

容器/板

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>
)

容器/ Controller

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

关于javascript - React/Redux - 将 Prop 从一个容器传递到另一个容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39550269/

相关文章:

linux - 如何在 Docker Hub 中过滤基于 Windows 或基于 Linux 的容器?

javascript - 将事件处理程序分配给提交按钮将引发SyntaxError : missing exponent error in Firebug

javascript - 如何在 typescript 文件中加载jquery模块?

javascript - 如何在这里使用 Find() : Vue. js

javascript - 如何使用 nextjs 将静态网站部署到 AWS S3

c++ - 在 C++ 中是否可以在不兼容类型的 std::vector 对象之间传输不同类型的缓冲区

php - 保存表单数据

javascript - 根据百分比值在两种颜色之间进行插值

javascript - React/Redux - 链接操作或组合 reducer

html - 我如何创建两个框来将内容放在其中,彼此相邻?