正如前言一样,我正在使用create-react-app样板。
我试图将wordpress API中的帖子解析为react组件,这样我就可以提取所有图像并使它们成为自己的JSX对象。我需要让每个帖子都有自己的on click方法,所以我不能在React中使用dangerouslyaddHTML函数。
目前,我搜索每个帖子的WP内容中呈现HTML的json字符串,并查找所有img标记。然后我将图像src添加到一个数组并在我的状态中设置它。
但是我收到此错误:
DOMException:仅允许安全的起源
我目前托管的网站http://natalie.madeline-omoore.com/build/
部分组件的代码如下:
`componentDidMount(){
var owner;
for(var i = 0; i < this.props.categoryId.length; i++) {
if (this.props.categoryId[i].num === this.props.content.categories[0]) {
owner = this.props.categoryId[i].category;
this.setState({
catname: owner
})
break;
}
}
var theContent = this.props.content.content.rendered;
var output = $(theContent).find("img").map(function() {
return this.src;
}).get();
this.setState({
postImages: output
});
}
click(){
this.updateActive();
this.props.fix();
}
render() {
if (this.props.className === this.state.catname) {
return (
<div >
{ this.props.className === this.state.catname &&
<article className={`${this.state.catname}`} >
{this.state.postImages.map((image, i) =>
<ImageContain key={image} image={image} fix={this.props.fix} />
)}
</article>
}
</div>
);
} else {
return null;
}
}`
我是新来的,所以谢谢你!
这听起来像是“服务工作者”的问题,默认情况下是create-react-app
您可以通过向index.js添加以下内容来取消注册该worker:
import { unregister } from './registerServiceWorker';
// ...
unregister();
确保在调用registerServiceWorker()之后调用unregister()。
你可以在这里了解更多相关信息:https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#making-a-progressive-web-app
祝你好运,编码愉快!