React JS 组件显示 [object Object] 而不是 JSX。

huangapple go评论69阅读模式
英文:

React JS-components showing [object Object] instead of JSX

问题

我正在创建一个具有预测文本、自动校正等功能的简单文本编辑器。我尝试更改用户输入中位于引号内的部分的格式,但由于某种原因,React 组件始终显示 [object Object],而不是呈现 JSX 元素。我肯定做错了什么。非常感谢任何帮助。

以下是处理此功能的函数:

import React from "react";

export default function HandleMonospace(ref, setText) {
    let text = ref;
    if (text) {
        const arraytext = text.split(' ');
        let newText = arraytext.map((item) => {
            if (/[^"]+"[^"]+"/.test(item)) {
                let moditem = item.split('');
                moditem.splice(0, 1);
                moditem.splice(moditem.length - 1, 1);
                moditem = moditem.join('');
                return <code>{moditem}&nbsp;</code>;
            } else {
                return `${item} `;
            }
        });
        newText = newText.join('');
        setText(newText);
    }
}

这里,ref 代表 ref.current.innerHTML

被渲染的组件是 contentEditable

对于以下输入:

Today seems to be a "good" day because the sky is somewhat "clear"

它会渲染成:

Today seems to be a [object Object]day because the sky is somewhat [object Object]

我尝试过更改数据类型,不使用状态变量来存储和显示输入,以及使用 textarea 元素。

英文:

I'm creating a simple text editor with features like predictive texting, autocorrection etc. I'm trying to change the format of parts of the user's input that are within quotations but for some reason the react component keeps showing [object Object] instead of rendering the JSX element. I'm definitely doing something wrong. Any and all help is deeply appreciated

Here's the function handling this feature

import React from &quot;react&quot;

export default function HandleMonospace(ref, setText){
    let text = ref
    if(text){
        const arraytext= text.split(&#39; &#39;)
        let newText= arraytext.map((item)=&gt;{
            if(/&quot;[^&quot;]+&quot;/.test(item)){
                let moditem= item.split(&#39;&#39;)
                moditem.splice(0, 1)
                moditem.splice(moditem.length-1, 1)
                moditem=moditem.join(&#39;&#39;)
                return &lt;code&gt;{moditem}&amp;nbsp;&lt;/code&gt;
            }
            else{
                return `${item} `
            }
        })
        newText=newText.join(&#39;&#39;)
        setText(newText)
    }
}

here, ref represents ref.current.innerHTML

the component being rendered to, is contentEditable

for
Today seems to be a &quot;good&quot; day because the sky is somewhat &quot;clear&quot;

it renders
Today seems to be a [object Object]day because the sky is somewhat [object Object]

I've tried switching datatypes
Deferring from using state variables to store and show the input
Using textarea element

答案1

得分: 1

这是导致[object Object]join()

如果您将moditem包装在&lt;code&gt;块中,您不能使用join(),因为这会将其转换为字符串,导致[object Object]

由于React可以渲染数组,只需删除join()并渲染HandleMonospace返回的数组。

小演示:

  • 包括上面的修复
  • &lt;code&gt;块添加了一些颜色,以使其更清晰。
const Example = () => {

    function HandleMonospace(ref){
        let text = 'Today seems to be a "good" day because the sky is somewhat "clear"';
        if(text){
            const arraytext= text.split(' ')
            let newText= arraytext.map((item)=>{
                if(/"[^"]+"/.test(item)){
                    let moditem= item.split('')
                    moditem.splice(0, 1)
                    moditem.splice(moditem.length-1, 1)
                    moditem=moditem.join('')
                    return <code>{moditem}&nbsp;</code>
                }
                else{
                    return `${item} `
                }
            })
            return newText;
        }
    }
    
    return (
        <div>
            <h1>{'Example'}</h1>
            <em>{HandleMonospace()}</em>
        </div>
    )
}
ReactDOM.render(<Example />, document.getElementById("react"));
code { color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>
英文:

It's the join() that causing the [object Object].

If you wrap your moditem in a &lt;code&gt; block, you can't do join() since that will convert it to a string, resulting in the [object Object].

Since React can render arrays, just remove the join() and render the array returned by HandleMonospace


Small demo:

  • Including the fix above
  • Added some color to the &lt;code&gt; block to make it more clear

<!-- begin snippet: js hide: false console: true babel: true -->

<!-- language: lang-js -->

const Example = () =&gt; {

    function HandleMonospace(ref){
        let text = &#39;Today seems to be a &quot;good&quot; day because the sky is somewhat &quot;clear&quot;&#39;;
        if(text){
            const arraytext= text.split(&#39; &#39;)
            let newText= arraytext.map((item)=&gt;{
                if(/&quot;[^&quot;]+&quot;/.test(item)){
                    let moditem= item.split(&#39;&#39;)
                    moditem.splice(0, 1)
                    moditem.splice(moditem.length-1, 1)
                    moditem=moditem.join(&#39;&#39;)
                    return &lt;code&gt;{moditem}&amp;nbsp;&lt;/code&gt;
                }
                else{
                    return `${item} `
                }
            })
            return newText;
        }
    }
    
    return (
        &lt;div&gt;
            &lt;h1&gt;{&#39;Example&#39;}&lt;/h1&gt;
            &lt;em&gt;{HandleMonospace()}&lt;/em&gt;
        &lt;/div&gt;
    )
}
ReactDOM.render(&lt;Example /&gt;, document.getElementById(&quot;react&quot;));

<!-- language: lang-css -->

code { color: red; }

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;react&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

问题是newText应该返回一个数组,但是else块尝试向其添加一个空格。

else {
   return `${item} ` // 不正确的格式 
   return item
} 
/* 其他代码 */

// join方法用于数组,因此newText应始终返回一个数组
newText = newText.join('') 
setText(newText)
英文:

The issue is newText is supposed to return an array but the else block is trying to add a space to it.

else {
   return `${item} ` // incorrect format 
   return item
} 
/* other code */

// join works on array, so newText should always return an array
newText = newText.join(&#39;&#39;) 
setText(newText)

huangapple
  • 本文由 发表于 2023年7月10日 20:37:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定