LoginSignup
13
7

More than 5 years have passed since last update.

Reactで改行込みのデータを表示させたい時

Posted at

コンテナにスタイルを当てればOK。
CSSだけで完結するのでReact以外でも使える。

import React from 'react';
import ReactDOM from 'react-dom';

const data = "hoge\nfuga";
const styles = { whiteSpace: 'pre-line' }; // CSSで書く場合は { white-space: pre-line }

const App = ({ children }) => (
  <div style={styles}>{children}</div>
);

ReactDOM.render(<App>{data}</App>, document.getElementById('root'));

デモ

13
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
7