我使用react創建了一個類似於twitter的框。我在查看反應文檔中發現的幾個組件生命週期,但不知道應該使用哪一個來改善我的代碼性能:componentDidMount
或componentWillMount
?componentDidMount或componentWillMount我需要使用哪一個
當我在我的文本框中鍵入內容時,我在控制檯中看到一個打印文本框值的更新。任何人都可以幫助我理解在這種情況下使用哪種方法?
https://jsfiddle.net/c9zv7yf5/2/
class TwitterBox extends React.Component {
constructor(props) {
super(props);
this.state = { enteredTextBoxvalue : '' };
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState({enteredTextBoxvalue: event.target.value});
if((event.target.value).length > 3) {
this.setState({className : 'wholeContainer'});
//console.log("long characters");
}
}
render() {
return (<div>Hello {this.props.name}
<textarea className={this.state.className}
value={this.state.enteredTextBoxvalue}
onChange = {this.handleChange}>
there should be only 140 characters
</textarea>
</div>);
}
}
ReactDOM.render(
<TwitterBox name="World" />,
document.getElementById('container')
);
的官方文檔實際上解釋非常好使用哪種「生命週期法」,其中的情況:https://facebook.github.io/react /docs/react-component.html –
@TomVanRompaey嘿我讀通過文件,但不知道何時實施...這將是偉大的,如果你幫我... –