How to use whiteSpace: 'pre-wrap' on React

前端 未结 2 748
滥情空心
滥情空心 2021-02-13 17:22

How can I use the style whiteSpace: \'pre-wrap\' on React

I have a div that need to render the text using the format with spaces

render() {
   
2条回答
  •  执念已碎
    2021-02-13 17:29

    JSX collapses whitespaces, in this case you can use dangerouslySetInnerHTML like so

    var Component = React.createClass({
      content() {
        const text = `
          keep formatting
    
          keep spaces
       `;
    
        return { __html: text };
      },
    
      render: function() {
        return 
    } });

    Note: For new versions of React/JSX, there is no need to use dangerouslySetInnerHTML

    const App = () => (
      
    {` keep formatting keep spaces keep spaces `}
    ); ReactDOM.render(, document.getElementById('root'));
    
    
    

提交回复
热议问题