【发布时间】:2018-02-07 07:06:35
【问题描述】:
为了实现componentDidMount()的效果,我写了如下代码段:
class Blink extends Component {
constructor(props) {
super(props);
this.state = {output: 'c'};
}
componentWillMount() {
this.state.output += 'w';
}
render() {
this.state.output += 'r';
let display = this.state.output;
return (
<Text>{display}</Text>
);
}
componentDidMount() {
this.state.output += 'd';
this.setState();
}
}
export default class BlinkApp extends Component {
render() {
return (
<View>
<Blink text='I love to blink' />
<Blink text='Yes blinking is so great' />
<Blink text='Why did they ever take this out of HTML' />
<Blink text='Look at me look at me look at me' />
</View>
);
}
我预计会看到四行'cwrdr',原因有二:
执行顺序:constructor(props)->componentWillMount()->render()->componentDidMount()
componentDidMount() 中调用的 setState() 将再次调用 render()。
请分享您对我的代码和推理的想法。非常感谢。
【问题讨论】:
标签: reactjs react-native