我正在自学 p/react,但我不确定为什么我无法访问组件的状态。
我有一个组件:
const AView = (state,props) => (
<div>
<p>{state.a}</p>
<B />
</div>
)
class A extends Component {
constructor() {
super();
this.state = { a: 1 };
}
render(props,state) {
return <AView a={state.a}/>
}
}
const BView = (state,props) => (
<div>
<p>{state.b}</p>
</div>
)
class B extends Component {
constructor() {
super();
this.state = { b: 2 };
}
render(props,state) {
return <BView b={state.b}/>
}
}
这会呈现A
具有预期状态的组件1
,但不会呈现B
具有状态的2
组件(组件B
只是使用空<p>
标签呈现)。
但是,如果我使用另一种语法,我可以B
使用 state渲染组件2
:
class B extends Component {
...
render(props,state) {
return BView(props,state);
}
}
我在这里在概念上遗漏了一些东西,还是只是有一些我不知道的语法?我试过用谷歌搜索,但我真的不知道足够的术语来获得相关的搜索结果。