Nim*_*mmy 81 javascript frontend reactjs react-redux
作为React世界的初学者,我想深入了解当我使用时会发生{this.props.children}
什么以及使用相同的情况.它在下面的代码片段中的相关性是什么?
render() {
if (this.props.appLoaded) {
return (
<div>
<Header
appName={this.props.appName}
currentUser={this.props.currentUser}
/>
{this.props.children}
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
Sor*_*esa 124
什么甚至是'孩子'?
React文档说,您可以使用
props.children
代表"通用盒子"的组件,并且不会提前知道他们的孩子.对我来说,这并不是很清楚.我肯定对某些人来说,这个定义很有道理,但它并不适合我.我的简单解释
this.props.children
是,它用于在调用组件时显示开始和结束标记之间包含的内容.一个简单的例子:
以下是用于创建组件的无状态函数的示例.同样,因为这是一个函数,所以没有
this
关键字,所以只需使用props.children
const Picture = (props) => {
return (
<div>
<img src={props.src}/>
{props.children}
</div>
)
}
Run Code Online (Sandbox Code Playgroud)
该组件包含一个
<img>
接收一些props
然后显示的组件{props.children}
.每当调用此组件
{props.children}
时,也会显示该组件,这只是对组件的开始和结束标记之间的内容的引用.
//App.js
render () {
return (
<div className='container'>
<Picture key={picture.id} src={picture.src}>
//what is placed here is passed as props.children
</Picture>
</div>
)
}
Run Code Online (Sandbox Code Playgroud)
<Picture />
如果您调用它,而不是使用自关闭标记调用该组件,则可以使用完整的开始和结束标记<Picture> </Picture>
,然后在它之间放置更多代码.这使
<Picture>
组件与其内容分离,并使其更具可重用性.
T.J*_*der 20
我假设您在React组件的render
方法中看到了这一点(编辑:您编辑过的问题确实表明了这一点):
class Example extends React.Component {
render() {
return <div>
<div>Children ({this.props.children.length}):</div>
{this.props.children}
</div>;
}
}
class Widget extends React.Component {
render() {
return <div>
<div>First <code>Example</code>:</div>
<Example>
<div>1</div>
<div>2</div>
<div>3</div>
</Example>
<div>Second <code>Example</code> with different children:</div>
<Example>
<div>A</div>
<div>B</div>
</Example>
</div>;
}
}
ReactDOM.render(
<Widget/>,
document.getElementById("root")
);
Run Code Online (Sandbox Code Playgroud)
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
children
是React组件的一个特殊属性,它包含组件中定义的所有子元素,例如上面的divs
内部Example
.{this.props.children}
包括渲染结果中的那些孩子.
...使用相同的情况是什么
当你想直接在渲染输出中包含子元素时,你会这样做,不变; 如果你没有,那就没有了.
zel*_*o_a 16
props.children
表示调用/渲染组件时开始和结束标记之间的内容:
const Foo = props => (
<div>
<p>I'm {Foo.name}</p>
<p>abc is: {props.abc}</p>
<p>I have {props.children.length} children.</p>
<p>They are: {props.children}.</p>
<p>{Array.isArray(props.children) ? 'My kids are an array.' : ''}</p>
</div>
);
const Baz = () => <span>{Baz.name} and</span>;
const Bar = () => <span> {Bar.name}</span>;
Run Code Online (Sandbox Code Playgroud)
调用/调用/渲染Foo
:
<Foo abc={123}>
<Baz />
<Bar />
</Foo>
Run Code Online (Sandbox Code Playgroud)
BTR*_*BTR 13
这个已回答,但想添加一个优化示例。您可以内联分解以仅获取特定属性而不是整个 props 对象(这意味着您不必继续编写props
)。
const MyView = ({title, children}) => {
return (
<>
<h1>{title}</h1>
{children}
</>
);
}
Run Code Online (Sandbox Code Playgroud)
然后你会像这样使用它:
import { MyView } from './MyView';
const MyParentView = () => {
return (
<MyView title="Hello">
<h2>World</h2>
</MyView>
);
}
Run Code Online (Sandbox Code Playgroud)
最终结果将是 HTML,其呈现如下所示:
<div>
<h1>Hello</h1>
<h2>World</h2>
</div>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
59404 次 |
最近记录: |