一直在研究React并希望知道分离智能组件和愚蠢组件的最佳实践。下面的例子家长控制状态,但我已经把按钮ui渲染,如果这些进入孩子,并通过回调实现回父或是过度杀伤? thoughts..here是我的代码React智能和哑元组件
class Child extends React.Component {
constructor(props){
super(props);
}
render() {
return (
<div><p>I said {this.props.greeting} {this.props.count} times</p>
</div>
);
}
}
class Parent extends React.Component {
constructor() {
super();
this.state = {
count: 0,
greeting: "Hello"
};
}
sayHello() {
this.setState((prevState, props) => {
return {
count: prevState.count + 1,
greeting: "Hello"
}
}
)};
sayGoodBye() {
this.setState((prevState, props) => {
return {
count: this.count = 1,
greeting: "Goodbye"
}
}
)};
render() {
return (
<div>
<button onClick={() => this.sayHello() }>Say Hello</button>
<button onClick={() => this.sayGoodBye() }>Say Goodbye</button>
<Child count={this.state.count} greeting={this.state.greeting} />
</div>
)
}
}
ReactDOM.render(<Parent />, document.getElementById('app'));
这可能更适合[Code Review](https://codereview.stackexchange.com/)吗? –
更多最佳实践问题比代码审查,但我会把它放在那里也有任何建议 – Michaelh