一些事情:
*Stateless Functional Components没有state,lifecycle方法和this关键字。
*您需要连接General和Application组件,以便Application组件可以state使用 General的值component。
* 将Application组件设为 General 组件的子组件props,并在 中传递注释值,并通过Application访问该值props.comments。
像这样写:
class General extends Component {
constructor() {
super();
this.state = { comments: first_comment};
}
render(){
return (
<div>
<Application comments={this.state.comments}/>
</div>
)
}
}
const Application = (props) => {
return (
<div> Hello world beginner: {props.comments}</div>
);
};
render(<General/>, document.getElementById('container'));
检查工作示例:
class General extends React.Component {
constructor() {
super();
this.state = { comments: 'first_comment'};
}
render(){
return (
<div>
<Application comments={this.state.comments}/>
</div>
)
}
}
const Application = (props) => {
return (
<div> Hello world beginner: {props.comments}</div>
);
};
ReactDOM.render(<General/>, document.getElementById('container'));
<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>
<div id='container'/>