英文:
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} </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 "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)
}
}
here, ref represents ref.current.innerHTML
the component being rendered to, is contentEditable
for
Today seems to be a "good" day because the sky is somewhat "clear"
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
包装在<code>
块中,您不能使用join()
,因为这会将其转换为字符串,导致[object Object]
。
由于React可以渲染数组,只需删除join()
并渲染HandleMonospace
返回的数组。
小演示:
- 包括上面的修复
- 为
<code>
块添加了一些颜色,以使其更清晰。
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} </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 <code>
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
<code>
block to make it more clear
<!-- begin snippet: js hide: false console: true babel: true -->
<!-- language: lang-js -->
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"));
<!-- language: lang-css -->
code { color: red; }
<!-- language: lang-html -->
<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>
<!-- 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('')
setText(newText)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论