情景:
調(diào)用安卓原生,使用廣播方式給RN發(fā)消息,向數(shù)組中插入數(shù)據(jù),渲染RN頁面,導(dǎo)致點(diǎn)擊按鈕事件,需要等到渲染基本結(jié)束以后才會調(diào)用.
解決方案:
RN:
1.將需要渲染的頁面數(shù)據(jù)單獨(dú)拉出來,作為組件,組件單獨(dú)渲染不會使其他組件也一起渲染
2.更新A和B組件內(nèi)容需要通過ref 實(shí)現(xiàn)
Android:將發(fā)消息改為異步實(shí)現(xiàn),不能使用同步!!!!!
export default class Home1 extends Component {
render() {
console.log('渲染根');
return (
<View style={{backgroundColor: 'white', marginTop: 100}}>
<ComponentA ref={(ref) => this.A = ref}/>
<ComponentB ref={(ref) => this.B = ref}/>
</View>
);
}
}
class ComponentA extends Component {
constructor(props) {
super(props)
this.state = {
length:0
}
}
render() {
console.log('渲染A');
return (
<Text>{this.state.lenght}</Text>
)
}
}
class ComponentB extends Component {
constructor(props) {
super(props)
this.state = {
dataList:[],
}
}
render() {
console.log('渲染B');
return (
<FlatList
style={styles.middleView}
extraData={this.state}
data={this.state.dataList}
renderItem={this._renderItem.bind(this)}
keyExtractor={(item, index) => item}>
</FlatList>
)
}
}