[英]Unable to Render Link Component React using Foreach
我有一個單詞數組,沒有單詞,我正在嘗試使用數組中的值來構造一個句子,並且只使單詞可以點擊。
下面是數組的示例。 此數據存儲在名為getWordsFromTokens的常量中
[
{
"type": "non-word",
"value": ""
},
{
"type": "word",
"value": "One"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "Saturday"
},
{
"type": "non-word",
"value": ", "
},
{
"type": "word",
"value": "when"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "I"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "was"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "young"
},
{
"type": "non-word",
"value": ", "
},
{
"type": "word",
"value": "my"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "father"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "and"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "I"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "left"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "the"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "house"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "early"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "in"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "the"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "morning"
},
{
"type": "non-word",
"value": ", "
},
{
"type": "word",
"value": "when"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "it"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "was"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "still"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "blue"
},
{
"type": "non-word",
"value": "-"
},
{
"type": "word",
"value": "black"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "outside"
},
{
"type": "non-word",
"value": ". "
},
{
"type": "word",
"value": "The"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "grass"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "left"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "wet"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "marks"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "on"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "our"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "shoes"
},
{
"type": "non-word",
"value": ". "
},
{
"type": "word",
"value": "In"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "the"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "backyard"
},
{
"type": "non-word",
"value": ", "
},
{
"type": "word",
"value": "under"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "stones"
},
{
"type": "non-word",
"value": ", "
},
{
"type": "word",
"value": "we"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "dug"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "up"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "crawly"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "worms"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "and"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "laid"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "them"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "in"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "a"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "can"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "with"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "lumps"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "of"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "damp"
},
{
"type": "non-word",
"value": " "
},
{
"type": "word",
"value": "dirt"
}
]
這是我如何嘗試構建句子並僅使單詞可點擊
getWordsFromTokens.forEach(item => {
const htmlToAppend = item.type === 'word' ?
// `<a href='/word/${item.value}'>${item.value}</a>` :
<Link to="user" params={item.value} >{item.value}</Link>:
item.value
document.getElementById('new-sentence').innerHTML += htmlToAppend;
})
在瀏覽器上呈現的是看起來像這樣的東西而不是文本
`` [對象對象] [對象對象], [對象對象] [對象對象] [對象對象] [對象對象], [對象對象] [對象對象] [對象對象] [對象對象] [對象對象] [對象對象][對象對象][對象對象][對象對象][對象對象][對象對象],[對象對象][對象對象][對象對象][對象對象][對象對象]-[對象對象][對象對象]目的]。 [對象對象] [對象對象] [對象對象] [對象對象] [對象對象] [對象對象] [對象對象] [對象對象]。 [對象對象] [對象對象] [對象對象], [對象對象] [對象對象], [對象對象] [對象對象] [對象對象] [對象對象] [對象對象] [對象對象] [對象對象] [對象對象] [對象對象] [對象對象] [對象對象] [對象對象] [對象對象] [對象對象] [對象對象] [對象對象].[對象對象] [對象對象], [對象對象] [對象對象] [對象對象] [對象
``
當我使用 anchot 標簽時,它可以工作,但我想使用 Link 組件。 我的代碼可能做錯了什么?
您不會在 React 中使用forEach
,因為這是一種在 JSX 中不起作用的命令式方法。 使用map
創建一個 JSX 值數組,並將其作為 JSX 代碼返回。
const elems = [ { "type": "non-word", "value": "" }, { "type": "word", "value": "One" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "Saturday" }, { "type": "non-word", "value": ", " }, { "type": "word", "value": "when" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "I" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "was" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "young" }, { "type": "non-word", "value": ", " }, { "type": "word", "value": "my" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "father" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "and" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "I" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "left" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "the" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "house" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "early" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "in" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "the" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "morning" }, { "type": "non-word", "value": ", " }, { "type": "word", "value": "when" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "it" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "was" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "still" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "blue" }, { "type": "non-word", "value": "-" }, { "type": "word", "value": "black" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "outside" }, { "type": "non-word", "value": ". " }, { "type": "word", "value": "The" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "grass" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "left" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "wet" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "marks" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "on" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "our" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "shoes" }, { "type": "non-word", "value": ". " }, { "type": "word", "value": "In" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "the" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "backyard" }, { "type": "non-word", "value": ", " }, { "type": "word", "value": "under" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "stones" }, { "type": "non-word", "value": ", " }, { "type": "word", "value": "we" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "dug" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "up" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "crawly" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "worms" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "and" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "laid" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "them" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "in" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "a" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "can" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "with" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "lumps" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "of" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "damp" }, { "type": "non-word", "value": " " }, { "type": "word", "value": "dirt" } ] const Words = () => { return (<p>{elems.map(el => el.type === "word"? (<a href={"/"+el.value}>{el.value}</a>): el.value)}</p>) } ReactDOM.render(<Words />, document.getElementById("root"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="root"></div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.