简体   繁体   English

作为prop传递的React组件的行为

[英]Behavior of a React component passed as prop

In most cases, you pass down components to another component as children . 在大多数情况下,您将组件作为children组件传递到另一个组件。 But I've seen (for example in couple of UI libraries) that components can be passed using standard named props as well. 但是我已经看到(例如,在几个UI库中)组件也可以使用标准命名的props进行传递。 I tried that too but I encountered couple of limitations and I wasn't able to find anything about that in the documentation (how it's actually supposed to work). 我也尝试过,但是遇到了一些限制,我在文档中找不到任何有关它的信息(实际上应该如何工作)。

This is what I'm trying to achieve: 这是我要实现的目标:

const App = ({ status, data }) => {
  return <Layout
    sidebar={<CustomSidebar />}
    toolbar={<CustomToolbar />}
    content={status == 'loading' ? <LoadingBar status={status} /> : <Content data={data} />}
  />
};

const LoadingBar = ({ status }) => <span>Loading... (current status: {status})</span>

As you can see here, using just children is not enough, I want to pass down multiple components and it will be Layout' s job to put them into right places. 正如您在这里看到的,仅使用children是不够的,我想传递多个组件,将其放置在正确的位置是Layout'的工作。 Obviously, the reason why I'm doing it this way is that I want to re-use the Layout . 显然,我这样做的原因是我想重新使用Layout

But it doesn't work. 但这是行不通的。 The LoadingBar is correctly displayed but once the data is loaded ( status changes from loading to done ), the LoadingBar prints correctly the changed status ( done ) but is never replaced by the Content component. LoadingBar正确显示,但是一旦加载数据( statusloading更改为done ), LoadingBar正确打印更改后的状态( done ), 但永远不会被Content组件替换。 Actually, it should never print done , it should simply disappear. 实际上,它永远不应该done打印,而应该消失。

I did some experiments and it only worked when I was passing completely static components as props (ie the input wasn't changing). 我做了一些实验,并且只有当我将完全静态的组件作为props传递时(即输入没有改变),它才起作用。 This worked: 这工作:

const App = ({ status, data }) => {
  return <Layout
    sidebar={<CustomSidebar />}
    toolbar={<CustomToolbar />}
    content={<ContentWrapper />}
  />
};

const mapStateToProps = (state, props) => { ... };

const ContentWrapper = connect(mapStateToProps)(({ status, data }) => {
  return status == 'loading' ? <LoadingBar status={status} /> : <Content data={data} />;
});

As you can see, the ContentWrapper component is connected to redux store so it's updated completely independently on the parent's component. 如您所见, ContentWrapper组件已连接到redux存储,因此它完全在父组件上独立更新。

Is there a way how to get the initial example working? 有没有办法使最初的例子起作用? Or am I doing something wrong? 还是我做错了什么? Is this approach endorsed? 这种方法得到认可吗?

Edit: The whole thing was a bug in my code. 编辑:整个事情是我的代码中的错误。 The status never changed to done and it didn't cross my mind to actually test it because of the overall weird behavior of this bug. 由于此错误的整体怪异行为, status从未改变为done并且我没想到要进行实际测试。 See my answer bellow. 看下面我的回答。

In your initial example you're only passing one or the other of <LoadingBar> or <Content> . 在最初的示例中,您仅传递了<LoadingBar><Content> There's probably a bunch of variations of how to achieve what you want. 如何实现所需的目标可能会有很多变化。 One option would be to pass both of those elements as separate props and status as a prop and let <Layout> decide what to do with them. 一种选择是将这两个元素都作为单独的道具传递,并将status作为道具传递,并让<Layout>决定如何处理它们。 Or you could even do something like this if you want: 或者,您可以根据需要执行以下操作:

content={function (status, data) {
  return status == 'loading' ? <LoadingBar /> : <Content data={data} />;
}}

Your code seems right. 您的代码似乎正确。 Isn't it a state problem? 这不是国家问题吗?

content={status === 'loading' ? <LoadingBar /> : <Content data={data} />}

Your LoadingBar will disappear when status is changed. 状态更改后,您的LoadingBar将消失。 Then 100% it will be replaced by the Content but with an empty data prop. 然后,将100%替换为Content,但使用一个空的数据道具。 I would say that the data prop doesn't get populated at the right time? 我想说的是,数据道具没有在正确的时间填充? What happens if you console log out the data prop when the loading bar disappears? 如果在加载栏消失时控制台退出数据道具,会发生什么情况?

Is all of your state defined in a proper manner? 是否以适当的方式定义了所有状态?

Okay, apparently the problem was in my code, there is nothing wrong with React and it works exactly as you would expect. 好的,显然问题出在我的代码中,React没有任何问题,它的运行完全符合您的期望。 I implemented the whole example on jsfiddle to test it in a separated environment so you can check it out: http://jsfiddle.net/9u4nmcrn/9/ 我在jsfiddle上实现了整个示例,以在单独的环境中对其进行测试,因此您可以检出它: http : //jsfiddle.net/9u4nmcrn/9/

It works like a charm. 它像一种魅力。


To give a short explanation: In my real app, the status isn't a string, it's an object with two properites: isLoading (boolean indicating whether the request is in progress) and done (boolean indicating whether the request finished with success). 简要说明一下:在我的真实应用中, status不是字符串,而是一个具有两个属性的对象: isLoading (布尔值指示请求是否正在进行中)和done (布尔值指示请求是否成功完成)。 The problem was that I was testing done in my outer component and isLoading in the LoadingBar (it hides itself once isLoading flips to false). 问题是,我在测试done我的外部组件和isLoadingLoadingBar (它隐藏自身,一旦isLoading翻转为false)。 For some other reason the boolean done never flipped to true . 对于一些其他原因布尔done从来没有翻到true So as the result, the LoadingBar disappeared but it was never replaced with Content as done was still false. 因此结果是, LoadingBar消失了,但是它从未被Content取代,因为done仍然是错误的。 Unfortunately, I was from the very beginning convinced that there must be something wrong with React so I didn't do even the very basic debugging. 不幸的是,我从一开始就坚信React一定有问题,所以我什至没有做最基本的调试。

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

相关问题 当传递给反应中的功能组件时,Prop 是未定义的 - Prop is undefined when passed to functional component in react React - 如何将道具传递给作为道具传递的组件 - React - How to pass props to a component passed as prop 反应状态作为道具传递给子组件未显示 - React state passed as prop to child component not showing 如何渲染在 React 中作为 prop 传递的组件? - How to render a component that is passed as a prop in React? React - 传递给子组件时道具的值未准备好 - React - value of prop not ready when passed to child component 如何更改作为列传递给 React Table 的子组件道具的值? - How to change the value of a prop of Sub Component passed as a column to React Table? 传递给子组件的事件处理程序(prop)不能称为react native - Event Handler (prop) passed to child component cannot be called react native 如果在反应组件中传递未知道具,如何显示警告? - How to show warning if unknown prop is passed in react component? 反应:将作为道具传递的状态分配给组件作为变量阻止更新? - React: Assigning State Passed as Prop to Component as Variable Prevents Update? 我如何判断一个 prop 是否在 react 组件中传递 - How do I tell if a prop was passed in a react component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM