简体   繁体   English

在 React Bootstrap Button 组件中有条件地呈现的 JavaScript 模板字符串中插入 \n 以换行是无效的

[英]Inserting \n to break line in JavaScript template string conditionally rendered in React Bootstrap Button component is ineffective

Part of a React component of mine is made of a React Bootstrap Button component.我的 React 组件的一部分是由 React Bootstrap Button 组件组成的。 Within this one, I render a string conditionally depending on its toggled state, as per the following code:在这一个中,我根据以下代码根据其切换的 state 有条件地呈现一个字符串:

<Button 
                size="sm"
                className={!toggleBoolean ? 'clickable block' : 'cancel clickable block'}
                onClick={()=>setToggleBoolean(!toggleBoolean)}
            >
                {!toggleBoolean ? `[${publicCommunication.date}]\n《${publicCommunication.title}》`: '关'}
            </Button>

As you can see, I included the \n because I want to put date and title on separate lines in the button, which I also display as a block (instead of the default inline-block) for this purpose.如您所见,我包含了 \n 是因为我想将日期和标题放在按钮的不同行上,为此我也将其显示为一个块(而不是默认的内联块)。 The application runs without errors, and the style is applied.应用程序运行无误,并且应用了样式。 However, the conditionally rendered string still appears on a consecutive line, as in the screenshot below:但是,条件渲染的字符串仍然出现在连续的一行上,如下面的屏幕截图所示: 换行符应该在封闭的方括号之后开始。相反什么也没有发生。

What can I do to solve this problem?我该怎么做才能解决这个问题? (Neither inserting (既不插入
nor html entities - even by enabling them on the button - is of any help). html 实体——即使在按钮上启用它们——也没有任何帮助)。 As dependencies, I am using "bootstrap": "^5.2.3" and "react-bootstrap": "^2.7.0".作为依赖项,我使用“bootstrap”:“^5.2.3”和“react-bootstrap”:“^2.7.0”。

Thanks for your help!谢谢你的帮助!

This is the issue with how CSS wrap your text.这是 CSS 如何包装文本的问题。 But if you just want to break line, you can use the <br/> html tag.但是如果你只是想换行,你可以使用<br/> html 标签。 You can reference the below code:您可以参考以下代码:

 <Button size="sm" className={?toggleBoolean: 'clickable block'? 'cancel clickable block'} onClick={()=>setToggleBoolean(.toggleBoolean)} > {.toggleBoolean: <>`[${publicCommunication.date}]`<br/>`《${publicCommunication.title}》`</>: '关'} </Button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM