[英]React functional component not redering inside parent functional component
For reference, I have this StackBlitz Link .作为参考,我有这个StackBlitz Link 。 You can check output there not working.您可以检查 output 那里没有工作。
I have one TodoList Line Item
component and I want to render LineItem
component inside of TodoListLineItem
component.我有一个TodoList Line Item
组件,我想在TodoListLineItem
组件内呈现LineItem
组件。 I am Using react with functional components and hooks with typescript.我正在使用 typescript 与功能组件和挂钩做出反应。
<div className="container">
<TodoForm />
<TodoListLineItem>
<LineItem />
</TodoListLineItem>
</div>
As, shown above when i am trying to put <LineItem />
component inside of <TodoListLineItem>
then, LineItem component is not rendering inside of parent component.如上所示,当我尝试将<LineItem />
组件放在<TodoListLineItem>
内时,LineItem 组件不会在父组件内呈现。
My Question is how to render LineItem component inside of parent TodoListLineItem component?我的问题是如何在父 TodoListLineItem 组件内渲染 LineItem 组件?
LineItem
component is a child of TodoListLineItem
component. LineItem
组件是TodoListLineItem
组件的子组件。 You need to render LineItem
component inside TodoListLineItem
component in order to render LineItem
component.您需要在TodoListLineItem
组件内渲染LineItem
组件才能渲染LineItem
组件。
When you nest a component like LineItem
is nested inside TodoListLineItem
component, that nested component is passed as a prop to the surrounding component and is accessible inside the surrounding component using children
property on the props
object.当您将LineItem
之类的组件嵌套在TodoListLineItem
组件中时,该嵌套组件将作为 prop 传递给周围组件,并且可以使用props
object 上的children
属性在周围组件内部访问。
props.children
will be an array that contains all the children components of TodoListLineItem
component. props.children
将是一个数组,其中包含TodoListLineItem
组件的所有子组件。 You can render all the children of TodoListLineItem
by rendering props.children
in TodoListLineItem
component您可以通过在TodoListLineItem
组件中渲染TodoListLineItem
来渲染props.children
的所有子项
const TodoListLineItem: React.FC = (props) =>{
const [close, setClose] = useState(false);
return (
<div>
<label className="line-item-header"> All Todo List items </label>
{ props.children } {/* render all the children of TodoListLineItem */}
</div>
);
}
https://stackblitz.com/edit/react-ts-asvv7x https://stackblitz.com/edit/react-ts-asvv7x
You can learn about props.children
on React Docs - Composition vs Inheritance你可以在React Docs - Composition vs Inheritance上了解props.children
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.