qq_笑_17
2023-05-11 15:57:27
我是 JSX/React 的新手,如果有人問(wèn)過(guò)這個(gè)問(wèn)題,請(qǐng)先向我道歉,請(qǐng)讓我參考這個(gè)問(wèn)題。為了刪除一些內(nèi)聯(lián)代碼,我創(chuàng)建了一個(gè)函數(shù),它返回要在模板/JSX 中顯示的值,這工作正常,除非它需要返回一個(gè)字符串 + 一個(gè)元素(鏈接元素),該函數(shù)如下所示:let displayText = () => { let supportLink = <Link component={RouterLink} to={`/customers/${customer.id}/support`}>support</Link> return `We are unable to do something. If you have further questions, please contact our ${supportLink}` };實(shí)際上這看起來(lái)像We are unable to do something. If you have further questions, please contact our [object, object]如何在我的字符串中包含該元素并使其正確顯示?
1 回答

侃侃無(wú)極
TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個(gè)贊
您正在返回一個(gè)模板字符串,因此supportLink被強(qiáng)制轉(zhuǎn)換為字符串,因此顯示[object, object]
您需要修改 return 語(yǔ)句以返回ReactElement如下所示:
let displayText = () => {
let supportLink = <Link component={RouterLink} to={`/customers/${customer.id}/support`}>support</Link>
return (<>
We are unable to do something. If you have further questions, please contact our {supportLink}
</>)
};
添加回答
舉報(bào)
0/150
提交
取消