[英]React.JS, how to render a component from it's child
I'm trying to make a nested tree of two components calling each other and 我正在尝试使两个相互调用的组件的嵌套树
I'm trying to make the following nested components tree 我正在尝试制作以下嵌套组件树
<Option>
<OptionContents>
<Option>
<OptionContents>
...
</OptionContents>
</Option>
</OptionContents>
</Option>
But I'm getting 'Option' undefined, I know that it's because OptionContents is loaded before Option which leads to the undefined error. 但是我得到的“选项”未定义,我知道这是因为OptionContents在Option之前加载,导致未定义的错误。 But is there a way to acheive this?
但是有没有办法做到这一点?
I don't see the problem if you're using a parent component ie: 如果您使用的是父组件,则看不到问题:
import Option
import OptionContents
render() {
return (
<Option>
<OptionContents>
<Option>
<OptionContents>
...
</OptionContents>
</Option>
</OptionContents>
</Option>
);
}
This wouldn't have a circular conflict afaik.. 这不会产生循环冲突。
I think I might understand what you're trying to do. 我想我可能会理解您正在尝试做的事情。 Assuming you have two components:
假设您有两个组成部分:
var Option = React.createClass({
render: function() {
return (<li>{this.props.children}</li>);
}
});
and 和
var OptionContents = React.createClass({
render: function() {
return (<div>{this.props.children}</div>);
}
});
And you'd like to use and re-use them (nesting and so on), you should import them both into a component which will render them. 而且,您想使用和重复使用它们(嵌套等),则应将它们都导入一个将呈现它们的组件中。
// import Option
// import OptionContents
var App = React.createClass({
render: function() {
return (
<Option>
<OptionContents>
<Option>
<OptionContents>
...
</OptionContents>
</Option>
</OptionContents>
</Option>
);
}
});
As opposed to include them in each other as dependencies and trying to render. 而不是将它们彼此作为依赖项并尝试呈现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.