javascript - 从函数返回带有 html 的字符串以在 JSX 中使用
问题描述
我是 JSX/React 的新手,如果有人问过这个问题,请先向我道歉,请参考这个问题。尝试删除一些内联代码,我创建了一个函数,该函数返回要在模板/JSX 中显示的值,这工作正常,除非它需要返回一个字符串 + 一个元素(链接元素),该函数如下所示:
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}`
};
在实践中,这看起来像
We are unable to do something. If you have further questions, please contact our [object, object]
如何在我的字符串中包含该元素并使其正确显示?
解决方案
您正在返回一个模板字符串,因此它supportLink
被强制为字符串,因此显示[object, object]
您需要修改 return 语句以返回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}
</>)
};
推荐阅读
- r - compareRaster(c(x, rasters)) 中的错误:不同程度
- amazon-ec2 - 当我将 gp2 EBS 卷转换为 gp3 时,后端会发生什么?
- python - 我正在写一个字母计数器,想知道如何让字典在底部显示一次(python)
- node.js - Vue:处理同时保存到数据库的有状态组件的最佳方式?
- amazon-web-services - CloundFront 重定向请求并删除查询参数
- python - Qt Designer 的标签问题?
- loops - 如何将嵌套 for 循环中的最后一行修改为 void 方法
- c++ - c++ 从 p7b 读取和获取数据
- c++ - 如何从派生类实例调用基类方法?
- java - 如何在 Azure-pipeline Ubuntu 代理上安装 java 13 并在 Maven 构建期间使用它?