javascript - 当我的 javascript 呈现时,为什么我的 <br/> 标记显示为 [object Object]?

标签 javascript html reactjs

我需要在句子之间添加中断标签,但是当我这样做时,我会得到 [object Object] 来代替换行符。有人可以告诉我如何解决这个问题吗?

const openingText = 'We\'re happy to hear your project is underway. '
 + <br /> +   'You can review this business to tell others about your experience.';

浏览器中的示例:

enter image description here

开发工具中的示例:

enter image description here

当我这样写时,出现语法错误:

const openingText = (
  <>
      We're happy to hear your project is underway.
      <br /><br />
      You can review this business to tell others about your experience.
  </>
);

enter image description here

当我在引号内添加 br 标签时,它只是认为它是常规文本:

const openingText = 'We\'re happy to hear your project is underway. <br /><br />' + 
  'You can review this business to tell others about your experience.';

enter image description here

最佳答案

因为您无法添加字符串对象:

var tmp = {}
console.log('This is an object: ' + tmp)

你想要用 React 做的是:

const openingText = (
    <>
        We're happy to hear your project is underway.
        <br />
        You can review this business to tell others about your experience.
    </>
);

如果使用该语法时出现错误,则您可能使用的是旧版本的 React 或使用不支持 React Fragment short syntax 的工具。 ,在这种情况下您应该能够执行以下操作:

const openingText = (
    <React.Fragment>
        We're happy to hear your project is underway.
        <br />
        You can review this business to tell others about your experience.
    </React.Fragment>
);

关于javascript - 当我的 javascript 呈现时,为什么我的 <br/> 标记显示为 [object Object]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54934604/

相关文章:

reactjs - react 钩子(Hook) : Display global spinner using axios interceptor?

javascript - 使用字符串作为 GPS 坐标

javascript - ajax 加载的 html 中的多个 jQuery 插件实例

javascript - 带复选框的标签内的 IE 按钮

css - 将 float 旁边的 div 居中对齐

javascript - 只检测鼠标滚动一次

javascript - 单击当前组件时删除类名称同级组件,并在当前组件中进行切换以删除其自身的类

javascript - 如何从 PouchDB 导入/导出数据库

javascript - 读取列表中特定元素的数据

javascript - 使用逻辑运算符有条件地启用/禁用按钮元素