久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

javascript - 為什么這種情況state改變不刷新頁面?

瀏覽:97日期:2024-02-20 08:01:59

問題描述

先說背景:頁面上是一個巨大的flatlist,里面有3個item,我在第三個item內部的最下面也就是整個外層flatlist的最下面有一個flatlist,這個flatlist加載的是state中的數據,需要做上拉加載功能,因為內部的flatlist拿不到下拉事件,所以我在最外層寫了上拉加載的方法,獲取到數據放到state中,這樣就改變了里面的數據。但是,雖然state變了,頁面卻沒刷新。后臺打印確定state改變了

上個簡要的代碼

constructor(props) {super(props)this.state = { data: [{'key': 1}, {'key': 2}, {'key': 3}],}this._changeData = this._changeData.bind(this); } _changeData(){this.setState({ data :[{'key': 1}, {'key': 2}, {'key': 3},{'key': 4},{'key': 5}],}) } _renderItem = ({item}) => {switch (item.key) { case 1:return ( <View><Text style={{height: 200}}>1111</Text> </View>); case 2:return ( <View><Text style={{height: 200}}>ke2222y2</Text> </View>); case 3:return ( //這個flatlist需要做上拉加載 <FlatListdata={this.state.data} renderItem={({item}) => <View style={{height: 200}}><Text>{item.key}</Text></View>} />)} } render() {const {navigate} = this.props.navigation;let user = {’name’: ’zy’, age: 18}return ( <View><Text onPress={() => navigate(’Two’, {user: user})}>Home</Text><FlatList data={[{'key': 1}, {'key': 2}, {'key': 3}]} renderItem={this._renderItem} onEndReached={this._changeData} onEndReachedThreshold={0.3}/><Text onPress={() => navigate(’Two’, {user: user})}>Home</Text> </View>) }

我寫的這個demo是可以實現的但是白天在項目里,那個數據是從網絡獲取過來的一個數組我用一個新的數組a把state里的數組放進去,再把拿過來的數組也放進去,最后把這個數組a賦值給state但發現并沒有變化,頁面沒有刷新

問題解答

回答1:

順便問一下有什么更好的方法

回答2:

你只能通過setState去觸發render。

this.setState({ data: anotherData})

不能通過直接賦值去觸發

this.state.data = anotherData

雖然不知道你真正實現是怎樣的,但我估計你用了后者這種方式。

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 漯河市| 安义县| 白水县| 宾阳县| 松桃| 邯郸县| 阜平县| 黑河市| 铜川市| 曲松县| 蓬安县| 邛崃市| 铜梁县| 屏山县| 温州市| 新竹县| 化州市| 万载县| 米林县| 资溪县| 乐山市| 怀柔区| 合川市| 巴里| 稻城县| 多伦县| 衡南县| 陇南市| 蒙自县| 油尖旺区| 嵩明县| 南木林县| 绥中县| 贵州省| 宁乡县| 屯昌县| 西峡县| 黔江区| 黑山县| 靖宇县| 格尔木市|