将值传递给子组件(this.props.children)

时间:2018-04-28 05:04:41

标签: javascript reactjs

我在布局中有一个子组件,我想传递一个道具值。但我不知道怎么做。在下面的类中,layoutFileDataRequest()从click事件的子组件接收字符串变量。需要将该值发送到this.props.children组件之一,以便它可以更新。

我该怎么做?在下面的代码中React.cloneElement(child, {不会改变它始终保持不变,这意味着我无法更新子道具。

  export default class Layout extends React.Component {
    constructor(props) {
      super(props)

      this.layoutFileDataRequest = this.layoutFileDataRequest.bind(this);
      this.state = {
        newData: '',
        returnData: 'test',

      }
    }

    /**
     *  Received request from server add it to 
     *  react component so that it can be rendered
     */
    layoutFileDataRequest(data) {
      this.setState({ returnData:data })
    }


    renderChildren() {
      return React.Children.map(this.props.children, child => {
        console.log(this.state.returnData); 
          return React.cloneElement(child, {
            data: this.state.returnData
          })
      });
    } 

    /**
     *  Render request
     * 
     * 
     */
    render() {
      const { location } = this.props;
      const title = this.state.newData;
      return (
        <div id="app-container" class={title}>
          <Nav location={location} />
          <main id="main">
            <h1>{title}</h1>
            <section>
                {this.renderChildren()}
            </section>
          </main>
          <Project layoutFileDataRequest={this.layoutFileDataRequest} />
          <Footer />
        </div>
      );
    }
  }


export default class Project extends React.Component {
  constructor(props) {
    super(props)

    this.projectFileDataRequest = this.projectFileDataRequest.bind(this);

    this.state = {
      newData: [],
    }

  }


  /**
   *  Received request from server add it to 
   *  react component so that it can be rendered
   */
  projectFileDataRequest(data) {
    this.props.layoutFileDataRequest(data);
  }


  /**
   *  Received request from server add it to 
   *  react component so that it can be rendered
   */
  componentDidMount() {
    ApiCalls.readSassDirData()
      .then(function (serverData) {
        this.setState({ newData: serverData[0].data })
      }.bind(this));
  }


  /**
   *  Render request
   */
  render() {
    const listOfObjects = this.state.newData;
    return (
      <aside id="project">
        <h2>Files</h2>
        <FileListing listOfObjects={listOfObjects} projectFileDataRequest={this.projectFileDataRequest} />,
       </aside>
    );
  }
}

3 个答案:

答案 0 :(得分:3)

我认为错误发生在renderChildren函数中。

<强>代码

 renderChildren() {
      return React.Children.map(this.props.children, child => {
        console.log(this.state.returnData); 
          return React.cloneElement(child, {
            data: this.state.returnData
          })
      });
    }

新代码

renderChildren(){
    return this.props.children.map(child => 
        React.cloneElement(child, { 
            data: {...this.state.returnData }
        })
    );
}

答案 1 :(得分:0)

实际代码没有任何问题,我猜测的唯一事情就是在<FileListing />组件中没有并且调用prop(方法)projectFileDataRequest传递更新我希望将它转移到<Layout />的孩子身上。

TL; DR ... 以下是您的代码在<Layout />收到点击事件后正在运行并更新<FileListing />的孩子。

https://codesandbox.io/s/5050w0p9kx

我希望它可以帮到你

答案 2 :(得分:-1)

您的代码实际上运行正常。这是cloneElement的参数:

React.cloneElement(
  element,
  [props],
  [...children]
)

您将数据作为道具传递给子组件。

检查一下:stackblitz.com/edit/react-xhwmd3?file=Layout.js