【发布时间】:2017-11-30 23:14:22
【问题描述】:
当前行为:
我正在尝试通过拉起视图来更新从服务器获取的列表。当我这样做时,onRefresh 不会触发。
我在 setState 函数的回调中设置了 GET 请求,但这似乎没有任何作用。
预期行为:
上拉视图调用 onRefresh 函数。
代码:
...
constructor(props) {
super(props);
this.state = {
stories: [],
isFetching: false,
};
}
componentDidMount() { this.fetchData() }
onRefresh() {
this.setState({ isFetching: true }, function() { this.fetchData() });
}
fetchData() {
var that = this;
axios.get('http://192.168.0.13:3000/api/story/get/by/geo')
.then((res) => {
that.setState({ stories: res.data, isFetching: false });
that.props.dispatch(StoryActions.setStories(res.data))
})
}
render() {
return (
<ScrollView>
<FlatList
onRefresh={() => this.onRefresh()}
refreshing={this.state.isFetching}
data={this.state.stories}
keyExtractor={(item, index) => item.id}
renderItem={({item}) => (<StoryFeed story={item} id={item.id} /> )}
/>
</ScrollView>
)
}
版本信息
反应原生:0.45.0
节点:7.4.0
【问题讨论】:
-
我认为
onRefresh是错误的放置位置。为什么不把它贴在render()中呢?也许我误解了,但在我看来,使用这样的刷新功能有点违背功能代码的全部目的...... -
根据文档 (facebook.github.io/react-native/docs/flatlist.html) onRefresh 是 FlatList 的一个属性。不知道你会如何渲染。
-
这很好,但我认为
onRefresh不会在您认为它会被调用时被调用。在函数式编程中,例如带有 React+Redux 的 JS6,您不会更改状态,而是创建一个新状态。您的状态树没有改变,因此没有创建新的状态。 -
尝试在控制台刷新一些东西,我想你会发现它永远不会在你当前的代码中被击中。
标签: react-native react-native-flatlist