JSX 문법으로 개행을 처리하는 방법을 몰라 꽤나 고생을 하여 기록합니다.
우선 props로 개행 문자인 '\n' 가 아닌 <br /> 태그를 전달해야합니다.
이후에는 dangerouslySetInnerHtml 와 함께 정규문법 RegExp를 사용하여 <br /> 태그를 '\n'로 치환해주면 됩니다.
예시는 아래와 같습니다.
Main.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react';
import Example from './Example';
function Main() {
return (
<Example message="안녕하세요<br/>잘 부탁드립니다." />
);
}
export default Main;
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react'; | |
import Example from './Example'; | |
function Main() { | |
return ( | |
<Example message="안녕하세요<br/>잘 부탁드립니다." /> | |
); | |
} | |
export default Main; |
Example.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
function Example(props) { | |
<h1> | |
<div dangerouslySetInnerHtml={{__html: props.message.replace(new RegExp('\n', 'g'), '<br/>')}} /> | |
</h1> | |
} | |
export default Example; |
반응형
'[DEV] 기록' 카테고리의 다른 글
통신사별 DNS 서버 IP 주소 정리 (0) | 2020.06.16 |
---|---|
C++ 이차원 배열 memset 함수를 통해 초기화하는 방법 (2) | 2020.06.02 |
React url 링크 클릭 시 새 탭으로 페이지 띄우는 방법 (0) | 2020.05.20 |
스프링 HTTP PUT/DELETE 메서드 차단하는 방법 (0) | 2020.05.17 |
Ant Style Pattern 정리 (6) | 2020.05.17 |