Reactjs 从Accordion列表中的api获取数据
我想从你那里拿到书名 在react native的手风琴视图中 我该怎么做 这是我的代码,但使用数组显示静态数据 现在我想显示来自api的数据Reactjs 从Accordion列表中的api获取数据,reactjs,react-native,react-native-android,Reactjs,React Native,React Native Android,我想从你那里拿到书名 在react native的手风琴视图中 我该怎么做 这是我的代码,但使用数组显示静态数据 现在我想显示来自api的数据 const dataArray = [ { title: "First Element", content:"" }, { title: "Second Element", content: "content1" }, { title: "Third Element",
const dataArray = [
{
title: "First Element",
content:""
},
{
title: "Second Element",
content:
"content1"
},
{
title: "Third Element",
content:
"content2"
}
];
class LocationScreen extends Component {
componentDidMount(){
return fetch('https://facebook.github.io/react-native/movies.json ')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
dataSource: responseJson.data,
}, function(){
});
})
.catch((error) =>{
console.error(error);
});
}
render() {
return (
<Container>
<Content padder>
<Accordion
dataArray={dataArray}
/>
</Content>
</Container>
);
}
const数据数组=[
{
标题:“第一要素”,
内容:“”
},
{
标题:“第二要素”,
内容:
“内容1”
},
{
标题:“第三要素”,
内容:
“内容2”
}
];
类位置屏幕扩展组件{
componentDidMount(){
返回获取('https://facebook.github.io/react-native/movies.json ')
.then((response)=>response.json())
.然后((responseJson)=>{
这是我的国家({
孤岛加载:false,
数据源:responseJson.data,
},函数(){
});
})
.catch((错误)=>{
控制台错误(error);
});
}
render(){
返回(
);
}
用{this.state.dataSource}替换{dataArray}
render() {
return (
<Container>
<Content padder>
<Accordion
dataArray={this.state.dataSource}
/>
</Content>
</Container>
);
}
render(){
返回(
);
}
根据@slashsharp的建议,使用this.state.dataSource
在render方法中,您使用的是静态数据{dataArray}。
如果要开始显示这些静态值,请使用以下命令:
const dataArray = [
{
title: "First Element",
content:""
},
{
title: "Second Element",
content:
"content1"
},
{
title: "Third Element",
content:
"content2"
}
];
class LocationScreen extends Component {
state = {
dataSource: dataArray, //init state with your static data
}
componentDidMount(){
return fetch('https://facebook.github.io/react-native/movies.json ')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
dataSource: responseJson.data,
}, function(){
});
})
.catch((error) =>{
console.error(error);
});
}
render() {
return (
<Container>
<Content padder>
<Accordion
dataArray={this.state.dataSource} // changed to this.state.dataSource
/>
</Content>
</Container>
);
}
const数据数组=[
{
标题:“第一要素”,
内容:“”
},
{
标题:“第二要素”,
内容:
“内容1”
},
{
标题:“第三要素”,
内容:
“内容2”
}
];
类位置屏幕扩展组件{
状态={
dataSource:dataArray,//init状态与静态数据
}
componentDidMount(){
返回获取('https://facebook.github.io/react-native/movies.json ')
.then((response)=>response.json())
.然后((responseJson)=>{
这是我的国家({
孤岛加载:false,
数据源:responseJson.data,
},函数(){
});
})
.catch((错误)=>{
控制台错误(error);
});
}
render(){
返回(
);
}
更改
this.setState({
数据来源:responseJson.movies
}
并使用dataArray={this.state.dataSource}
renderContent(节,i活动){
返回(
{section.text}//从API获取
);
}
componentDidMount(){
const url=“url”;
获取(url)
.then(response=>response.json())
.然后(responseJson=>{
这是我的国家({
数据源:responseJson.data
});
})
.catch(错误=>{
console.log(错误);
});
}
render(){
返回(
);
}
将您的dataArray更改为this.state.datasource您能解释一下您的答案吗?这将有助于OP或未来的读者更好地阅读。