简体   繁体   English

将 x state 变量传递给 y {props.children}

[英]React pass x state variables to y {props.children}

I'm using functional components.我正在使用功能组件。 I have built my separate components and am placing them into my index.js file.我已经构建了单独的组件并将它们放入我的index.js文件中。

Right now the structure of the components is (pseudocode):现在组件的结构是(伪代码):

<App stateVariable1={x} stateVariable2={y} stateVariable3={z}>
  <ChildOne props1={x} props2={y} />
  <ChildTwo props1={z}/>
</App>

ChildOne and ChildTwo are currently rendered within App.js , and so passing the state variables to the two children is very easy. ChildOne 和 ChildTwo 目前在App.js中呈现,因此将 state 变量传递给两个孩子非常容易。

I'd like to extract the rendered children into index.js and replace the two children with a {props.children} within App.js , to increase the SRP of the component and make it more reusable.我想将渲染的子元素提取到index.js中,并将两个子元素替换为 App.js 中的 { App.js } ,以增加组件的 SRP 并使其更具可重用性。

However I am struggling with understanding how to pass multiple state variables as multiple props to props.children within my index.js file.但是,我很难理解如何将多个 state 变量作为多个道具传递给我的index.js文件中的props.children

I have read the react documentation for Render Props and that deals with one prop being passed to a single child, rather than multiple children which want to receive different props.我已经阅读了 Render Props 的 react 文档,其中处理了一个道具被传递给一个孩子,而不是多个想要接收不同道具的孩子。

How do I go about achieving what I would like to do?我如何 go 关于实现我想做的事情?

UPDATE更新

I have attempted to use render props as per the accepted answer here: How to pass props to {this.props.children}我已尝试按照此处接受的答案使用渲染道具: 如何将道具传递给 {this.props.children}

My index.js looks like this:我的index.js看起来像这样:

ReactDOM.render(
  <React.StrictMode>
    <App>
      {(stateVariable1) => (
        <Fragment>
          <ChildOne props1={stateVariable1} props2={stateVariable2} />
          <ChildTwo props3={stateVariable3} />
        </Fragment>
      )}
    </App>{' '}
  </React.StrictMode>,
  document.getElementById('root')
);

However all of the props/stateVariables through undefined errors, as of course they're not defined in index.js .然而,所有的 props/stateVariables 都存在未定义的错误,因为它们当然没有在index.js中定义。

Where am I going wrong?我哪里错了?

Update 2: Solution更新 2:解决方案

I passed all of the state variables as arguments to the render props and this has solved the issue:我将所有 state 变量作为 arguments 传递给渲染道具,这解决了问题:

ReactDOM.render(
  <React.StrictMode>
    <App>
      {(stateVariable1, stateVariable2, stateVariable3) => (
        <Fragment>
          <ChildOne props1={stateVariable1} props2={stateVariable2} />
          <ChildTwo props3={stateVariable3} />
        </Fragment>
      )}
    </App>{' '}
  </React.StrictMode>,
  document.getElementById('root')
);

Is there a way to deconstruct this so that I don't need to pass each argument into the callback?有没有办法解构它,这样我就不需要将每个参数传递给回调? I am using functional components so state is stored in multiple variables rather than within the class components' state object.我正在使用功能组件,因此 state 存储在多个变量中,而不是存储在 class 组件的 state ZA8CFDE63311BD59EB2AC96B8 中。

I think you can avoid having to use props.children altogether.我认为您可以避免完全使用props.children

If you have a structure like below, your SRP principle will still hold true.如果您有如下结构,您的SRP原则仍然适用。

  • App应用程序
    • Child 1孩子 1
    • Child 2孩子 2

What the above lines mean is that rather than going with render props pattern or having props.children , you can have your Child1 and Child2 as components within your App component.以上几行的意思是,您可以将Child1Child2作为App组件中的组件,而不是使用 render props 模式或props.children

Something like就像是

const App = (props) => {
 return (
   <>
     <Child1 {...props} />
     <Child2 {...props} />
   </>
 );
}

If you want to make code more generic and have a single Children component, you can extract all children out to index.js file so that your App component remains untouched, but that might not be needed to be honest.如果您想让代码更通用并拥有一个Children组件,您可以将所有子组件提取到index.js文件中,以便您的App组件保持不变,但老实说可能不需要这样做。

I solved my issue with some help from Tyblitz.我在 Tyblitz 的帮助下解决了我的问题。

ReactDOM.render(
  <React.StrictMode>
    <App>
      {({stateVar1, stateVar2, ...stateVars}) => (
        <Fragment>
          <ChildOne props1={stateVar1} props2={stateVar2} />
          <ChildTwo {...stateVars} />
        </Fragment>
      )}
    </App>{' '}
  </React.StrictMode>,
  document.getElementById('root')
);

I then added a useState() function to my App component and passed all state variables in as an object.然后,我将useState() function 添加到我的App组件,并将所有 state 变量作为 object 传递。

Finally, I passed this new state variable in as an argument to my return call:最后,我将这个新的 state 变量作为参数传递给我的返回调用:

return <div className='App'>{props.children(stateVars)}</div>;

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

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