【发布时间】:2020-06-11 13:16:48
【问题描述】:
以下是生成一系列按钮的代码。当初始渲染发生时,一个函数确定元素是否有一个名为 init 的 prop。如果这样做,它会执行该操作,就好像该按钮已被单击一样。
从技术上讲,这段代码可以工作,但它会触发警告,因为它在渲染过程中有效地触发了重新渲染。如何触发有效的 OnRender 函数?
export class NavTabItem extends React.Component {
constructor(props) {
super(props);
global.register(this, 'screen')
}
NavTabAction = () => {
global.setState({
screen: this.props.screen,
})
}
render() {
// determine whether the element has the prop of init and if it does click on it.
if(this.props.init){
this.NavTabAction()
}
return (
<TouchableOpacity onPress={this.NavTabAction}>
<View style={global.state.screen == this.props.screen ? [styles.NavTabItem, styles.NavTabItemSelected] : styles.NavTabItem}>
<View style={styles.NavTabIcon} />
<TextCap11 style={styles.NavTabLabel}>{this.props.children}</TextCap11>
</View>
</TouchableOpacity>
);
}
}
【问题讨论】:
标签: function react-native render