ํฐ์คํ ๋ฆฌ ๋ทฐ
Issue
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
class Comments extends Component {
render() {
let { name, comment } = this.props;
let isSkip = false;
let addVal;
if (comment.length > 20) {
comment = comment.slice(0, comment.length - 6);
isSkip = true;
addVal = (
<span style={{ color: '#dee2e6', fontSize: '6px' }}> ๋๋ณด๊ธฐ...</span>
);
}
return (
<li className="comment-item">
<div className="comment">
<Link to="/login/choi" className="feed-bottom-account profile-name">
{name}
</Link>
<span className="comment-content">{comment}</span>
{isSkip && addVal}
</div>
<button className="comment-heart">
<img
src="https://s3.ap-northeast-2.amazonaws.com/cdn.wecode.co.kr/bearu/heart.png"
alt="heart"
/>
</button>
</li>
);
}
}
export default Comments;
๋ค์ ์ฝ๋๋ comment ๋์ ์๋ก์ด ๋๊ธ์ ๋๋๋งํ๋ ์ปดํฌ๋ํธ์ด๋ค.
props ๋์ด ์จ ๋๊ธ์ ๊ธธ์ด๊ฐ 20์ ๋์ด์๊ฒ ๋๋ฉด ๋๊ธ์ ๋ท ๋ด์ฉ์ ์ญ์ ํ๊ณ '...๋๋ณด๊ธฐ' ๋ผ๋ ์๋ก์ด ๋ฌธ์๋ฅผ ์ถ๊ฐํด์ฃผ๋ ์ฐ์ฐ ๊ณผ์ ์ render() ํจ์์์ ์ ์ธํด์ฃผ์๋ค.
๊ทธ ๊ณผ์ ์์ ์คํ์ผ์ ๋์ ์ผ๋ก ์์ฑํด์ฃผ๊ธฐ ์ํด์ Inline-style ์์ฑ์ ์ฌ์ฉํ์๋๋ฐ
addVal = ( <span style={{ color: '#dee2e6', fontSize: '6px' }}> ๋๋ณด๊ธฐ...</span> );
ํด๋น ์ฝ๋๋ render() ํจ์ ์์์ ์ด๋ ํ ๊ณผ์ ์ด ์ด๋ฃจ์ด์ ธ์ผ ํ๋ฉฐ ๋ฌด์์ด return๋๋์ง ๋ค์ ์๊ฐํด๋ดค์ ๋, ์ข์ง ๋ชปํ ์ฝ๋์์ ๊นจ๋ฌ์๋ค.
render() ํจ์๋ ํ๋ฉด ์์ ์ค์ ๋ก ๋ณด์ฌ์ง๋ ๊ฒฐ๊ณผ๋ฌผ์ ๋ฆฌํดํด์ผ ํ๋ฉฐ ๋ฆฌํด ๋ ๊ฒฐ๊ณผ๋ฌผ ๋ฐ๋ผ ๋๋๋ง๋๋ค.
JSX๋ HTML ๋ฌธ๋ฒ์ JS ์์์ ํํํ ๊ฒ์ด๊ธฐ ๋๋ฌธ์, ๊ธฐ๋ณธ์ ์ผ๋ก ๋งํฌ์ ํ์(๊ตฌ์กฐ)์ผ๋ก ๊ตฌ์ฑ๋๋ค.
ํ์ง๋ง ํ์ฌ addVal ๋ณ์์ ํ ๋น ๋ ํ๊ทธ ๋ด์๋ Inline-style ์์ฑ์ด ํฌํจ๋์ด ์๊ณ ์ด๋ css ์์ style ์์ญ์ด๋ค.
์ด๋ฐ ์์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ์ฌ๋ ์ค์ ๋๋๋ง๋๋ ๊ณผ์ ์์๋ ๋ณ ๋ฌธ์ ๊ฐ ์์ง๋ง ๊ด์ฌ์ฌ ๋ถ๋ฆฌ ๊ด์ ์์ ๋ดค์ ๋, ๋ฆฌ์กํธ์ ๋์์ ์ฑ๋ฅ์ ์ ํ์ํฌ ์ ์๋ ์์ธ์ด ๋ ์ ์๋ค.
์ปดํจํฐ ๊ณผํ์์ ๊ด์ฌ์ฌ ๋ถ๋ฆฌ(separation of concerns, SoC)๋ ์ปดํจํฐ ํ๋ก๊ทธ๋จ์ ๊ตฌ๋ณ๋ ๋ถ๋ถ์ผ๋ก ๋ถ๋ฆฌ์ํค๋ ๋์์ธ ์์น์ผ๋ก, ๊ฐ ๋ถ๋ฌธ์ ๊ฐ๊ฐ์ ๊ด์ฌ์ฌ๋ฅผ ํด๊ฒฐํ๋ค.
ํนํ, Inline-style๊ณผ ๊ฐ์ ๊ฒฝ์ฐ๋ ์คํ์ผ ์ ์ฉ ์ฐ์ ์์๊ฐ ๋ด๋ถ ์คํ์ผ(internal style)๊ณผ ์ธ๋ถ ์คํ์ผ(external style)๋ณด๋ค ๋๊ธฐ ๋๋ฌธ์ ์๋์น ์์ ์คํ์ผ์ด ์ ์ฉ๋ ๊ฒฐ๊ณผ๋ฌผ์ด ๋๋๋ง๋ ๊ฐ๋ฅ์ฑ์ด ์๋ค.
์ด์ ๊ฐ์ ์ด์ ๋ก, ์คํ์ผ์ ๋์ ์ผ๋ก ์ถ๊ฐํด์ผ ๋๋ ๊ฒฝ์ฐ์๋ ๋ค์๊ณผ ๊ฐ์ด className์ css-selector๋ฅผ ์ง์ ํด์ฃผ๋ ๋ฐฉ์์ผ๋ก ์ฝ๋๋ฅผ ๋ถ๋ฆฝํ์ฌ ๊ตฌ์ฑํ๋ ๊ฒ์ด ๋ ์ข์์ ํ์ธํ ์ ์์๋ค.
addVal = ( <span className="isSkipWord"> ๋๋ณด๊ธฐ...</span> );
.isSkipWord { color: '#dee2e6', fontSize: '6px' }
'React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Type ๊ฐ์ ๋ฐ๋ผ ์กฐ๊ฑด๋ถ ๋๋๋ง_getDerivedStateFromProps (0) | 2021.11.07 |
---|---|
Carousel Slide ๊ธฐ๋ฅ Component๋ก ๋ถ๋ฆฝ (0) | 2021.11.07 |
๋ช ๋ นํ ํ๋ก๊ทธ๋๋ฐ๊ณผ ์ ์ธํ ํ๋ก๊ทธ๋๋ฐ(how / what) (0) | 2021.10.28 |
๋ฆฌ์กํธ ๋ฉ์๋ ๋ด this ๋ฐ์ธ๋ฉ ๋ฌธ์ (0) | 2021.10.24 |
๋ฆฌ์กํธ ํ๋ก์ ํธ์ ์น ํฐํธ ์ ์ฉํ ๋ GlobalStyles์ ๋ฌธ์ ์ (0) | 2021.10.24 |