在 React Native 中更新变量和设置状态时遇到问题
Trouble with updating variable and set state in react native
我有这个功能:
async function search() {
try {
const response = await places.nearbysearch({
location: text, // LatLon delimited by ,
radius: distance, // Radius cannot be used if rankBy set to DISTANCE
type: ['cafe','bakery','meal_delivery','meal_takeaway','restaurant'], // Undefined type will return all types
// rankby: "distance" // See google docs for different possible values
});
const { status, results, next_page_token, html_attributions } = response;
var index = Math.floor(Math.random() * response.results.length);
console.log(response.results[index].name);
restName = response.results[index].name;
console.log(restName);
} catch (error) {
console.log(error);
}
}
在export default function App() {}
之内
我在视图中创建了一个文本组件,如下所示:
return (
<View style={styles.container}>
<View style={{minWidth: '100%', minHeight: '100%', backgroundColor: '#f0fbff', alignFit:'stretch', marginTop: 0, alignItems:'center', justifyContent:'space-evenly'}}>
<Card style={{padding: 5, margin: 1}}>
<Text style={{ fontWeight: 'bold', fontSize: 25, }}>{restName}{"\n"}</Text>
即使 restName 更新(我制作了一个按下时运行 search() 函数的按钮),文本也没有更新,我将如何用它实现设置状态或刷新变量。当我 console.log restName 时,它显示正确的内容,所以我知道变量已更新,只是文本没有更新。
简而言之,实际变量发生变化但显示没有任何不同
UPDATE 我尝试使用 setstate,但它仍然没有更新这里是功能
var restName = "";
async function search() {
try {
const response = await places.nearbysearch({
location: text, // LatLon delimited by ,
radius: distance, // Radius cannot be used if rankBy set to DISTANCE
type: ['cafe','bakery','meal_delivery','meal_takeaway','restaurant'], // Undefined type will return all types
// rankby: "distance" // See google docs for different possible values
});
const { status, results, next_page_token, html_attributions } = response;
var index = Math.floor(Math.random() * response.results.length);
console.log(response.results[index].name);
this.setState({ restName : response.results[index].name })
console.log(restName);
} catch (error) {
console.log(error);
}
}
要更新状态,您不能只重新分配
restName = response.results[index].name
但必须打电话setState
this.setState({ restName : response.results[index].name })
我有这个功能:
async function search() {
try {
const response = await places.nearbysearch({
location: text, // LatLon delimited by ,
radius: distance, // Radius cannot be used if rankBy set to DISTANCE
type: ['cafe','bakery','meal_delivery','meal_takeaway','restaurant'], // Undefined type will return all types
// rankby: "distance" // See google docs for different possible values
});
const { status, results, next_page_token, html_attributions } = response;
var index = Math.floor(Math.random() * response.results.length);
console.log(response.results[index].name);
restName = response.results[index].name;
console.log(restName);
} catch (error) {
console.log(error);
}
}
在export default function App() {}
我在视图中创建了一个文本组件,如下所示:
return (
<View style={styles.container}>
<View style={{minWidth: '100%', minHeight: '100%', backgroundColor: '#f0fbff', alignFit:'stretch', marginTop: 0, alignItems:'center', justifyContent:'space-evenly'}}>
<Card style={{padding: 5, margin: 1}}>
<Text style={{ fontWeight: 'bold', fontSize: 25, }}>{restName}{"\n"}</Text>
即使 restName 更新(我制作了一个按下时运行 search() 函数的按钮),文本也没有更新,我将如何用它实现设置状态或刷新变量。当我 console.log restName 时,它显示正确的内容,所以我知道变量已更新,只是文本没有更新。
简而言之,实际变量发生变化但显示没有任何不同
UPDATE 我尝试使用 setstate,但它仍然没有更新这里是功能
var restName = "";
async function search() {
try {
const response = await places.nearbysearch({
location: text, // LatLon delimited by ,
radius: distance, // Radius cannot be used if rankBy set to DISTANCE
type: ['cafe','bakery','meal_delivery','meal_takeaway','restaurant'], // Undefined type will return all types
// rankby: "distance" // See google docs for different possible values
});
const { status, results, next_page_token, html_attributions } = response;
var index = Math.floor(Math.random() * response.results.length);
console.log(response.results[index].name);
this.setState({ restName : response.results[index].name })
console.log(restName);
} catch (error) {
console.log(error);
}
}
要更新状态,您不能只重新分配
restName = response.results[index].name
但必须打电话setState
this.setState({ restName : response.results[index].name })