我想知道如何使用和使用此json,始终显示和更新列表,请在工作中使用它,而我正努力做到这一点。
答案 0 :(得分:1)
就像在React中一样。首先,您应该获取数据(通常是componentDidMount()
的好地方。您需要更新组件的状态以包含获取的数据。这是使用axios
的示例,但是可以达到相同的效果通过fetch
API实现。
class MyComponent extends Component {
state = {
data : []
}
componentDidMount(){
axios.get('myendpoint')
.then(res => this.setState({data : res}))
}
render(){
const { data } = this.state
return(
<FlatList
data={data}
renderItem={({item}) => <Text>{item}</Text>}
/>
)
}
}
使用fetch
class MyComponentWithFetch extends Component {
state = {
data : []
}
componentDidMount(){
fetch('myendpoint')
.then(docs => docs.json())
.then(res => this.setState({data : res}))
}
render(){
const { data } = this.state
return(
<FlatList
data={data}
renderItem={({item}) => <Text>{item}</Text>}
/>
)
}
}
ps:别忘了按键