一直在研究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