我有一個API,其中title, description, old price
和new price
應呈現,但我不知道如何正確的方式。在react.js中使用API呈現數據?
與我的API
export class Content extends React.Component{
constructor(){
super();
this.state={};
}
componentWillMount(){
var url="95.85.23.63:8000/frontend/web/api/v1/user/get-popular";
Request.get(url).then((reponse)=>{
this.setState({
infos:response.body.Search,
total:response.body.totalResults
});
});
}
render(){
var infos=_.map(this.state.infos, (info)=>{
return <li>{info.title}</li>;
});
return(
<div>
<ul> {infos}</ul>
</div>
)
}
}
下面內容組成部分我覺得我在一個錯誤的方式檢索數據。你可以幫我嗎?
您應該創建動作,然後要麼你可以使用反應-thunk或react-saga從api中檢索數據。當組件裝入時,意味着componentDidMount(){this.props.dispatch(search())} –