一些事情:
*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'/>