Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在字符串中引入react元素_Reactjs - Fatal编程技术网

Reactjs 在字符串中引入react元素

Reactjs 在字符串中引入react元素,reactjs,Reactjs,我有一个网站的区域设置内容作为一个道具,它是一个字符串值,我想用一个react元素替换其中的一些内容。但是,当我使用replace函数进行类似操作时,元素被转换为字符串,我在被替换部分的输出中看到[object object],而不是正在渲染的react元素。有什么方法可以做到这一点吗?下面的代码片段可以完成这项工作,并且足够灵活,可以满足您的需要。还解决了可能需要使用自定义_组件交换字符串的多个部分的情况 const localeString = "Locale content replace

我有一个网站的区域设置内容作为一个道具,它是一个字符串值,我想用一个react元素替换其中的一些内容。但是,当我使用replace函数进行类似操作时,元素被转换为字符串,我在被替换部分的输出中看到
[object object]
,而不是正在渲染的react元素。有什么方法可以做到这一点吗?

下面的代码片段可以完成这项工作,并且足够灵活,可以满足您的需要。还解决了可能需要使用自定义_组件交换字符串的多个部分的情况

const localeString = "Locale content replace_me_with_component and some text at the end";

const renderCustomComponent = () => <div>this is the custom component to include in the string</div>

const App = ({ localeString, ...props }) => {
    const splittedArrayOfStrings = localeString.split("replace_me_with_component");

    // You might want to add validations, in case the localeString does not contain your "special" string in it (replace_me_with_component)

    return splittedArrayOfStrings.map((string, index) => {
        // Inject the custom component into the string
        if (index < separatedArrayOfStrings.length - 1) {
            return (<>{string} {renderCustomComponent()}</>)
        }

        return <>{string}</>
    })
}

// Call the App component
<App localeString={localeString} />
const localeString=“Locale content将_me_替换为_组件和结尾的一些文本”;
const renderConstomComponent=()=>这是要包含在字符串中的自定义组件
常量应用=({localeString,…props})=>{
const splittedArrayOfStrings=localeString.split(“用_组件替换_me_”);
//您可能需要添加验证,以防localeString中不包含您的“特殊”字符串(将\u me\u替换为\u组件)
返回SplittedArrayOfString.map((字符串,索引)=>{
//将自定义组件注入字符串
if(索引
以下代码片段可以完成这项工作,并且足够灵活,可以满足您的需要。还解决了可能需要使用自定义_组件交换字符串的多个部分的情况

const localeString = "Locale content replace_me_with_component and some text at the end";

const renderCustomComponent = () => <div>this is the custom component to include in the string</div>

const App = ({ localeString, ...props }) => {
    const splittedArrayOfStrings = localeString.split("replace_me_with_component");

    // You might want to add validations, in case the localeString does not contain your "special" string in it (replace_me_with_component)

    return splittedArrayOfStrings.map((string, index) => {
        // Inject the custom component into the string
        if (index < separatedArrayOfStrings.length - 1) {
            return (<>{string} {renderCustomComponent()}</>)
        }

        return <>{string}</>
    })
}

// Call the App component
<App localeString={localeString} />
const localeString=“Locale content将_me_替换为_组件和结尾的一些文本”;
const renderConstomComponent=()=>这是要包含在字符串中的自定义组件
常量应用=({localeString,…props})=>{
const splittedArrayOfStrings=localeString.split(“用_组件替换_me_”);
//您可能需要添加验证,以防localeString中不包含您的“特殊”字符串(将\u me\u替换为\u组件)
返回SplittedArrayOfString.map((字符串,索引)=>{
//将自定义组件注入字符串
if(索引
你可以,但你应该向我们展示一些代码,看看你可以,但你应该向我们展示一些代码,看看我的用例,我暂时使用了
危险的HTML
,但我认为最好是编写一个函数,按照你提到的方式处理内容。感谢我的用例,我暂时使用了
危险的SetinenerHTML
,但我认为最好编写一个函数,按照您提到的方式处理内容。谢谢