Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- not 의사클래스
- iframe
- background-color 속성
- RGB
- padding 속성
- html
- 아두이노
- tag html
- 일반 형제 결합자
- iframe 태그
- 인접 형제 결합자
- i 태그
- go live
- height속성
- Checked 의사 클래스
- 자식결합자
- sup태그
- html 태그
- RGBA
- focus 의사클래스
- id 선택자
- Live Server
- 전체 선택자
- width속성
- reveal in file explorer
- br 태그
- css
- html tag i
- sub태그
- 임베디드
Archives
- Today
- Total
so woon!
[React] 동일한 css 속성을 다른 파일 내에서 사용하기 본문
학습일 : 2023. 03. 16
css 적용을 하며 같은 class 중복문제와
또 그걸 피하기 위해
다른 이름의 class 를 주고
같은 코드를 그대로 긁어 와서 사용했던
아주 비효율적이고 찝찝했던 지난날의 무거운 고민을
드디어 해결할 시간이 왔다.
가장 먼저, 준비되어야 할 파일은
1. Button.module.css
2. Button.js
3. index.js
4. App.js
준비가 되었다면 시작
<Button.module.css>
btn 클래스에 적용될 css를 작성해준다.
/* btn 클래스 만들기 */
/*create-react-app은 이 CSS 코드를 js 오브젝트로 변환시켜주게 된다. */
.btn {
color:rgb(255, 255, 255);
background-color: rgb(21, 102, 75);
}
<Button.js>
import styles from "./Button.module.css"
를 상단에 작성하여 css파일을 import 해준다.
그런 다음 <button> 태그 안에 className 을 부여해준다.
작성방법은
<button className={styles.btn}>...</button>
이런식으로 작성해주면 된다.
import PropTypes from "prop-types"; // import 해주면 밑에서 쓸 수 있다.
import styles from "./Button.module.css";
function Button({ text }) {
return <button className={styles.btn}>{text}</button>; // className 부여(랜덤으로 생성됨)
}
Button.propTypes = {
text: PropTypes.string.isRequired,
};
export default Button;
className을 styles.btn으로 주었기 때문에
개발자도구상으로 보면
class 이름이 랜덤으로 생성되어 중복이 되지 않는 편리함이 있다.
<index.js>
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
<App.js>
import Button from "./Button";
function App() {
return (
<div>
<h1>Welcome back!!!!</h1>
<Button text={"Continue"} />
</div>
);
}
export default App;
실행결과
'ReactJS > 개념정리' 카테고리의 다른 글
[React] 특정 코드가 변화했을 때만 코드를 실행시키는법 (0) | 2023.03.16 |
---|---|
[React] useEffect (0) | 2023.03.16 |
[React] CSS 적용하기 (0) | 2023.03.16 |
[React] propTypes 설치하기 (0) | 2023.03.16 |
[React] 관계성 살펴보기 (0) | 2023.03.16 |
Comments