我在更新状态时遇到问题。我使用地址集作为"newurl"获取JSON结果,并在FlatList中显示内容。这一切都很好,我有一个按钮,当点击更新'newurl'状态并重新加载数据。问题是需要点击两次来更新状态?我试过把setState在一个不同的类,但不工作,我也不能使新的'newurl'包括'newuser'的状态
更新状态和重新加载2次点击后更新的数据的onclick是:
onPress = () => {
this.setState({
newurl: 'https://www.newtesturl'
}),
fetch(this.state.newurl)
.then(response => response.json())
.then((response)=> {
this.setState({loading: false,list: response.data})
})
}
更新'newurl'字符串时的问题,我似乎无法将另一个数据状态添加到其中。如果我使用下面的代码,"newurl"几乎是一样的,没有添加"newuser"状态?
this.setState({
loading: true,
newurl: 'https://www.example.com/userid={this.state.newuser}'
});
这真的很烦人,因为当我把它添加到一个直接的url像下面它工作吗?
<Image source={{uri: `https://www.example.com/userid={this.state.newuser}`}} />
这就是我如何设置我的道具,以防它有什么不同?:
type Props = {};
export default class App extends Component<Props> {
constructor(props) {
super(props);
this.state = { list: [], loading: false, newuser: '10101', newurl: '' };
}
对不起,如果我没有解释得很好,但如果你需要任何澄清,请问,谢谢
状态更新可能是异步的。更多信息请看这里
第二个问题是因为你没有在那里使用模板字符串。更多信息请看这里