使用:Reaction、WordPress REST API、Axios

我从一个定制的POST类型中提取结果,当我获得文本框字段"acf.ther"的结果时,每当用户输入回车时,它都会放入<br />标记、</p> <p>标记或不放任何标记,从而导致连续的段落不带格式.如果它放置标签,那么结果会显示站点上的标签,而不是实际使用标签(见img).screenshot of results

export const SingleRecent = () => {
    const { singleSlug } = useParams();
    const [single,setSingle] = useState([]);

    useEffect(() => {
        axios.get(recent + singleSlug)
        .then(response => {
            setSingle(response.data);
        })
        .catch((error) => {
            console.log(error);
        })
    
    }, [singleSlug]);
    
    return (
        <><div className="pagebg singlePageBg">
            <main>
                <div className="container notHomeContainer singleContainer">
                    <div className="gallery notHomeGallery singleGallery">
                        {single.map(single_recent => <SingleRecentItems key={single_recent.id} single_recent={single_recent} />)}
                    </div>
                </div>
            </main>
        </div></>
    );
}

class SingleRecentItems extends Component {
   
    render() {
        let { acf } = this.props.single_recent;
        return (
            <>
                <h1>{acf.title}</h1>
                <h4>{acf.style}</h4>
                <div className="photos">
                    <span className="recentPhoto">
                        <img 
                            src={acf.photo}
                            alt={acf.title} 
                        />
                    </span>
                </div>
                <div className="otherInfo">
                    {acf.other}
                </div>
                <div className="location">
                    Photo Taken in: {acf.location_other}<br />
                    {acf.location_city}<br />
                    {acf.location_state}<br />
                    {acf.location_country}
                </div>
                <div className="keywords">
                    {acf.subject}
                </div>
            </>
        )
    }
}

我希望获得{acf.other}的结果,并将标记转换为功能标记,以便它将以用户希望的方式(或多或少)格式化结果.我可以以某种方式将{acf.other}赋给一个变量,解析和连接,然后返回结果吗?有没有更好的办法?

推荐答案

general规则是,您don't希望将您的数据视为代码.如果这是第三方数据,恶意用户可能会在您的网页中写入<script src="steal-their-cookies.xyz">,而您更愿意在页面上将其视为"<;脚本src="steal-They-cookies.xyz">;",而不是作为已执行代码!将用户生成的数据作为代码插入到页面中是使您expose 于跨站点脚本(XSS)攻击和其他页面 destruct 和数据窃取风险的一种很好的方法.

然而,在您的例子中,数据是supposed编码的,是来自可信来源的supposed.为了支持您的用例,Reaction提供了一个dangerouslySetInnerHTML property,允许您将这些数据视为代码.与在this LogRocket blog中一样,这对于让受信任的编辑提交富文本的CMS来说是有意义的,但您应该意识到危险并极其谨慎地使用该属性.

<!-- Beware the caveats above. -->
<div className="otherInfo" dangerouslySetInnerHTML={{__html: acf.other}} />

Reactjs相关问答推荐

单击按钮时在子元素中不显示任何内容-react

Formik验证不适用于物料UI自动完成

无法从正在使用Reaction的Electron 商务store 项目中的购物车中删除项目

如何在Reac.js中通过子组件和props 调用父组件函数?

当列表中的项的顺序改变并且使用唯一键作为它们的索引时,Reaction如何看待呈现方面?

这两个子元素在Reaction Native中使用的有什么不同?

在Reaction中的第一次装载时,Use Effect返回空数组

我无法通过 useContext 显示值

如何在 Next Js 13 App Router 中添加 Favicon 图标?

React - 使用 React 显示错误和积极alert

如何在 React map 函数循环中使用
标签

将 ref 转发到所有页面(路由组件)只是为了将其应用于
并具有跳过导航链接(按钮).有没有更好的办法?

为什么接受 useState 返回的函数的 setter 在使用 React 的单个调用中执行两次?

从 API 中提取映射数组后出现重复元素

如何解决在 react.js 中找不到模块错误

未捕获的错误:操作必须是使用 redux/toolkit 的普通对象

Material UI 安装和 React v. 18.2 出现问题

useEffect 仅在跟随链接后有效,不适用于直接链接

错误(未捕获的类型错误):无法读取未定义的属性(读取参数)

当上下文中的状态发生变化时,组件不会立即重新渲染