【发布时间】:2019-09-15 16:39:14
【问题描述】:
下面的代码是针对单个帖子的,axios 获取当前帖子 ID 并将结果存储在帖子数组中。请记住,仅存储一个帖子,因为这是获取单个帖子。 ID 和日期显示正确,但如果我尝试显示嵌套项目(如渲染内容),它将不起作用。
constructor(props) {
super(props);
this.state = {
post: []
};
}
getPost() {
axios
.get('single_post_api')
.then(response => {
this.setState({
post: response.data
});
});
}
componentDidMount() {
this.getPost();
}
render() {
const data = this.state.post;
return (
<View>
<Text>{data.date}</Text>
<Text>{data.slug}</Text>
///Neither of these work///
<Text>{data.content.rendered}</Text>
<Text>{data.content[0]}</Text>
////////////////////////////
</View>
);
}
【问题讨论】:
-
您不能只将html 作为反应中的值。
-
@HMR 我不确定你的意思是什么?
-
在数据中
content.rendered是html,你不能做<div>{content.rendered}</div>,但必须按照我前面提到的方法去做(因为xss攻击)。你可以试试<pre>{JSON.sringify(data,undefined,2)}</pre>看看你哪里做错了。
标签: reactjs react-native axios