Javascript 对象作为React子对象无效(找到:具有键{…}的对象)
我正在尝试获取一些示例数据,并在我的react原生应用程序中显示一些文本。这就是代码的外观(基本上直接取自教程): 我收到了错误信息Javascript 对象作为React子对象无效(找到:具有键{…}的对象),javascript,json,react-native,Javascript,Json,React Native,我正在尝试获取一些示例数据,并在我的react原生应用程序中显示一些文本。这就是代码的外观(基本上直接取自教程): 我收到了错误信息 如果我在函数调用之后放入toString(),则会打印出[object object],因此我想这是在返回之前将对象转换为文本的问题。有人能告诉我这是怎么做的吗?A.您返回的是调用getExampleData中的fetch方法返回的promise对象,而不是Star Wars B.如果您想要呈现标题。最好在componentWillMount方法中启动调用,然后
如果我在函数调用之后放入
toString()
,则会打印出[object object],因此我想这是在返回之前将对象转换为文本的问题。有人能告诉我这是怎么做的吗?A.您返回的是调用getExampleData
中的fetch
方法返回的promise对象,而不是Star Wars
B.如果您想要呈现标题。最好在componentWillMount
方法中启动调用,然后在到达成功回调后,使用运行render
方法的this.setState
方法设置状态
getExampleData() {
return fetch('https://facebook.github.io/react-native/movies.json')
.then((response) => response.json())
.then((responseJson) => {
return responseJson.movies[0].title;
})
.catch((error) => {
console.log(error);
return 'Error!';
});
}
componentWillMount() {
getExampleData().then(function (title) {
this.setState({
title
})
})
}
render() {
return (
<Text>{this.state.title}</Text>
);
}
getExampleData(){
返回获取('https://facebook.github.io/react-native/movies.json')
.then((response)=>response.json())
.然后((responseJson)=>{
返回responseJson.movies[0]。标题;
})
.catch((错误)=>{
console.log(错误);
返回'Error!';
});
}
组件willmount(){
getExampleData().then(函数(标题){
这是我的国家({
标题
})
})
}
render(){
返回(
{this.state.title}
);
}
A.您返回的是调用getExampleData
中的fetch
方法返回的承诺对象,而不是星球大战
B.如果您想要呈现标题。最好在componentWillMount
方法中启动调用,然后在到达成功回调后,使用运行render
方法的this.setState
方法设置状态
getExampleData() {
return fetch('https://facebook.github.io/react-native/movies.json')
.then((response) => response.json())
.then((responseJson) => {
return responseJson.movies[0].title;
})
.catch((error) => {
console.log(error);
return 'Error!';
});
}
componentWillMount() {
getExampleData().then(function (title) {
this.setState({
title
})
})
}
render() {
return (
<Text>{this.state.title}</Text>
);
}
getExampleData(){
返回获取('https://facebook.github.io/react-native/movies.json')
.then((response)=>response.json())
.然后((responseJson)=>{
返回responseJson.movies[0]。标题;
})
.catch((错误)=>{
console.log(错误);
返回'Error!';
});
}
组件willmount(){
getExampleData().then(函数(标题){
这是我的国家({
标题
})
})
}
render(){
返回(
{this.state.title}
);
}
JSON.stringify(…)因为您返回的是承诺(对象!),而不是字符串文字。JSON.stringify(…)因为您返回的是承诺(对象!),而不是字符串文字。
getExampleData() {
return fetch('https://facebook.github.io/react-native/movies.json')
.then((response) => response.json())
.then((responseJson) => {
return responseJson.movies[0].title;
})
.catch((error) => {
console.log(error);
return 'Error!';
});
}
componentWillMount() {
getExampleData().then(function (title) {
this.setState({
title
})
})
}
render() {
return (
<Text>{this.state.title}</Text>
);
}