admin管理员组文章数量:1356875
I save more strings inside an array. What I want to do is to show them when hover over an icon each of them on separate lines. What I tried so far:
addMessages = () => {
const text = [];
//add strings in the array
return text.join("<hr/>");
}
render() {
const showWhenHover = this.addMessages();
return (
<ActionBar popover={<div> {showWhenHover}</div>}
<div>
<Icon type="myIcon"/>
</div>
</ActionBar>
);
}
}
When I hover over the icon it shows the messages but not each of them on a separate line but all in one line like this:
text1</hr>text2</hr>text3
Isn't <hr/>
what must be used in this case? Thanks
I save more strings inside an array. What I want to do is to show them when hover over an icon each of them on separate lines. What I tried so far:
addMessages = () => {
const text = [];
//add strings in the array
return text.join("<hr/>");
}
render() {
const showWhenHover = this.addMessages();
return (
<ActionBar popover={<div> {showWhenHover}</div>}
<div>
<Icon type="myIcon"/>
</div>
</ActionBar>
);
}
}
When I hover over the icon it shows the messages but not each of them on a separate line but all in one line like this:
text1</hr>text2</hr>text3
Isn't <hr/>
what must be used in this case? Thanks
-
1
Sounds like you're looking for
<br>
– user5734311 Commented Jun 20, 2017 at 9:30 - I tried also with that but what I think is that it doesn't recognise the tag, it takes it like plain text – Samurai Jack Commented Jun 20, 2017 at 9:31
- showWhenHover all returns a text node. If you will have it render as markup you want to return a React.Component instance. – Oluwafemi Sule Commented Jun 20, 2017 at 9:32
-
return (<div>{text.map((txt, i) => <div key={i}>{txt}<br></div> )}</div>);
inaddMessages
– Oluwafemi Sule Commented Jun 20, 2017 at 9:35
3 Answers
Reset to default 4Your function addMessage generates strings and not a html markup.
One solution is to use template literals allow multiline strings. The other thing is that make sure that the text are contained within an element that has defined dimensions, or dimensions big enough that the text can go to the next line.
const genText = () => `
Text with lots of
spaces within
it blah blah blah
blah
`
const Comp = () => <div style={{width: 100, height: 200}}>{genText()}</div>
ReactDOM.render(<Comp />, document.getElementById('app'))
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>
text.join
will render a single string, including <hr />
in this case. In order to render JSX instead, try:
addMessages = () => {
const text = [];
// add strings in the array
return text.map((item, index) => (
<span key={index}>
{item}
{index && <hr />}
</span>
));
}
Only downside here is the extra span, but I would prefer this over using dangerouslySetInnerHTML
.
You can also use return text.map(e => <div>{e}</div>);
to get each string in its own line.
function addMessages() {
const text = [];
text.push("1st line");
text.push("2nd line");
text.push("Third line");
text.push("And a final one");
return text.map(e => <div>{e}</div>);
}
const App = () => <div>{addMessages()}</div>
ReactDOM.render(<App />, document.getElementById('app'))
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>
本文标签:
版权声明:本文标题:javascript - Break a text into multiple lines in React - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743959351a2568731.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论