简体   繁体   English

使用来自json文件的字符串将字符串数组传递给React组件,并允许内联span标签

[英]Pass array of strings to React component with string coming from json file and allow inline span tags

I'm trying to get an array of strings to display as paragraphs and allow an inline span tag inside of these strings. 我正在尝试获取一个字符串数组以显示为段落,并允许在这些字符串中使用内联span标签。

My issue comes is when the value is added inside the paragraph the decodes the "<" and ">" tag start and ends to their decoded values "<" and ">" 我的问题是,当将值添加到段落中时,将解码“ <”和“>”标记开始,并结束其解码后的值“ <”和“>”

Is there an easy way to get this working without making a specific component for this case? 有没有一种简单的方法就可以在不为此情况创建特定组件的情况下使它正常工作?

My React component is as follows 我的React组件如下

const Paragraphs = ({ data, languageText }) => {
    if (data) {
        const texts = languageText[data.languageKey];

        return (
            <section>
                {texts && texts.map(text => <p>{text}</p>)}
            </section>
        );
    } else {
        console.warn(`webpage::element: 'PARAGRAPHS' lack content`);
    }
}
export default Paragraphs;

this is the array that is passed to texts 这是传递给文本的数组

[
"When a Relic is found or assembled from Relic Fragments, a payout is rewarded, based on its rarity. <span style=\"font-weight: bold\">The rarer the item, the higher the payout!</span>",
"In addition to Relics, Gold Coins can also be found."
],

默认情况下,React会转义HTML以防止XSS只使用dangerouslySetInnerHTML使用SetInnerHTML ,请看官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除功能 <span></span> 从JavaScript中的json对象数组中的字符串 - Function to remove <span></span> from string in an json object array in JavaScript 如何使用字符串合并跨度标签并使用jQuery将其添加到数组中 - How to club span tags from a string and add in to an array using jquery 从字符串数组动态响应调用组件 - React call Component dynamically from array of strings 如何将 Json 对象数组从子 class 组件传递给 React 中的父 class 组件? - How to pass a Json array of objects from a child class component to parent class component in React? 从 Javascript/React Native 中的多个字符串数组中删除 html 标记 - Removing html tags from multiple strings array in Javascript/React Native 如何使用道具和反应挂钩将来自后端的 json 数组传递给子组件? - How to pass a json array that comes from backend to child component using props and react hooks? 为什么我的反应组件中的 span 元素打印在新行上而不是内联? - Why is my span element in a react component printing on a new line and not inline? React:内联有条件地将 prop 传递给组件 - React: inline conditionally pass prop to component 从 Json 读取数据并填充 Span 标签 - Read data from Json and populate Span tags 如何在反应中将对象数组从父组件传递到子组件 - How to pass Array of Objects from Parent Component to Child Component in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM