[英]React functional component not redering inside parent functional component
作为参考,我有这个StackBlitz Link 。 您可以检查 output 那里没有工作。
我有一个TodoList Line Item
组件,我想在TodoListLineItem
组件内呈现LineItem
组件。 我正在使用 typescript 与功能组件和挂钩做出反应。
<div className="container">
<TodoForm />
<TodoListLineItem>
<LineItem />
</TodoListLineItem>
</div>
如上所示,当我尝试将<LineItem />
组件放在<TodoListLineItem>
内时,LineItem 组件不会在父组件内呈现。
我的问题是如何在父 TodoListLineItem 组件内渲染 LineItem 组件?
LineItem
组件是TodoListLineItem
组件的子组件。 您需要在TodoListLineItem
组件内渲染LineItem
组件才能渲染LineItem
组件。
当您将LineItem
之类的组件嵌套在TodoListLineItem
组件中时,该嵌套组件将作为 prop 传递给周围组件,并且可以使用props
object 上的children
属性在周围组件内部访问。
props.children
将是一个数组,其中包含TodoListLineItem
组件的所有子组件。 您可以通过在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
你可以在React Docs - Composition vs Inheritance上了解props.children
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.