【发布时间】:2017-09-22 08:00:53
【问题描述】:
我想禁用 setState 消息警告
在布局 1 上: 我的构造函数:
constructor(props){
super(props);
//Constructeur
this.lestachesitemsRef=getDatabase().ref('n0rhl66bifuq3rejl6118k8ppo/lestaches'); this.lestachesitems=[];
this.state={
//Debutdustate
lestachesSource: new ListView.DataSource({rowHasChanged: (row1, row2)=>row1 !== row2}),
Prenom: '',
Nom: '',}
}
我的功能:
ajouter= () => {
if( (this.state.Nom !== '') && (this.state.Prenom !== '')) { this.lestachesitemsRef.push({ Nom: this.state.Nom , Prenom: this.state.Prenom , }); this.setState({ Nom : '' }) , this.setState({ Prenom : '' }) }
}
我的组件DidMount
componentDidMount()
{
//DidMount
this.lestachesitemsRef.on('child_added', (dataSnapshot)=>{ this.lestachesitems.push({id: dataSnapshot.key, text: dataSnapshot.val()}); this.setState({lestachesSource: this.state.lestachesSource.cloneWithRows(this.lestachesitems)}); });
this.lestachesitemsRef.on('child_removed', (dataSnapshot)=>{ this.lestachesitems = this.lestachesitems.filter((x)=>x.id !== dataSnapshot.key); this.setState({ lestachesSource: this.state.lestachesSource.cloneWithRows(this.lestachesitems)});});
}
我的 :
<TextInput style={styles.Dtext} placeholder="Nom" onChangeText={(text) => this.setState({Nom: text})} value={this.state.Nom}/>
<TextInput style={styles.Dtext} placeholder="Prenom" onChangeText={(text) => this.setState({Prenom: text})} value={this.state.Prenom}/>
<Button title='Allerlayout2' onPress={() => this.verl2()} onLongPress={() => this.infol2()} buttonStyle={ styles.View } icon={{name: 'squirrel', type: 'octicon', buttonStyle: styles.View }} />
<Button title='ajouter' onPress={() => this.ajouter()} onLongPress={() => this.infoajout()} buttonStyle={ styles.View } icon={{name: 'squirrel', type: 'octicon', buttonStyle: styles.View }} />
<ListView dataSource={this.state.lestachesSource} renderRow={this.renderRowlestaches.bind(this)} enableEmptySections={true} />
Layout2 和 Layout1 一样。 谢谢!
【问题讨论】:
-
你可能不想禁用它,它看起来很糟糕(我猜是异步相关的)。介意分享代码以便我们更好地帮助您吗?
-
@FerranNegre 我添加了代码谢谢!
标签: react-native components warnings setstate