Search

React Style

React Style

React μ—μ„œ μŠ€νƒ€μΌλ§ν•˜λŠ” 방법을 μ•Œμ•„λ΄…λ‹ˆλ‹€.

React μ—μ„œ μŠ€νƒ€μΌλ§ ν•˜λŠ” 방법

β€’
β€’
BootStrap
β€’
React-BootStrap

inline style

μ»΄ν¬λ„ŒνŠΈμ— style 속성에 직접 μŠ€νƒ€μΌ 속성을 μ •μ˜ν•˜λŠ” 방법

문법

<μ»΄ν¬λ„ŒνŠΈ style={ { 속성1 : 'κ°’1', 속성2: 'κ°’2' } } />
JavaScript
볡사
μ»΄ν¬λ„ŒνŠΈμ˜ style 속성을 μ •μ˜ν•˜μ—¬ { } ν‘œν˜„μ‹ μ•ˆμ—μ„œ, { } 객체의 ν˜•νƒœλ‘œ μŠ€νƒ€μΌμ„ μ§€μ •ν•©λ‹ˆλ‹€.
β€’
μŠ€νƒ€μΌ 속성 : 일반적으둜 CSS μŠ€νƒ€μΌ μ‹œνŠΈμ—μ„œλŠ” μŠ€νƒ€μΌ 속성을 μΌ€λ°₯ μΌ€μ΄μŠ€(”style-property”) 둜 μ§€μ •ν•˜μ§€λ§Œ, React 의 μŠ€νƒ€μΌ 속성은 카멜 μΌ€μ΄μŠ€(”styleProperty”) 둜 μ§€μ •ν•©λ‹ˆλ‹€.
β€’
μŠ€νƒ€μΌ κ°’ : β€˜κ°’β€™ λ˜λŠ” β€œκ°’β€ λ”°μ˜΄ν‘œλ‘œ λ¬Άμ–΄μ„œ μ§€μ •ν•©λ‹ˆλ‹€.

μ˜ˆμ‹œ μ½”λ“œ

인라인 μŠ€νƒ€μΌμ„ μ μš©ν•˜μ—¬ λ²„νŠΌμ„ λ§Œλ“€μ–΄ λ΄…λ‹ˆλ‹€.

κΈ°λ³Έ λ²„νŠΌ

Button.jsx
import React from 'react' const Button = () => { return ( <button style={{ backgroundColor: 'royalblue', color: 'white', fontWeight: 'bold', outline: 'none', border: 'none', margin: '0 2px', padding: '10px 24px', borderRadius: '6px', cursor: 'pointer', }}>Button</button> ) } export default Button
JavaScript
볡사

μ»€μŠ€ν…€ λ²„νŠΌ

CustomButton.jsx
import React from 'react' const CustomButton = ({ text, btnStyle, btnSize, onClick }) => { let color = 'white'; let backgroundColor = 'black'; let padding = '12px 24px'; let display = 'inline-block'; let fontSize = '14px'; let width = 'auto'; let margin = '0 4px'; switch (btnStyle) { case 'primary': backgroundColor = '#007BFF'; break; case 'secondary': backgroundColor = '#6C757D'; break; case 'success': backgroundColor = '#28A745'; break; case 'danger': backgroundColor = '#DC3545'; break; case 'warning': backgroundColor = '#FFC107'; color = 'black'; break; case 'info': backgroundColor = '#17A2B8'; break; case 'light': backgroundColor = '#F8F9FA'; color = 'black'; break; case 'dark': backgroundColor = '#343A40'; break; default: break; } switch (btnSize) { case 'xs': padding = '2px 8px'; fontSize = '10px'; break; case 'sm': padding = '6px 12px'; fontSize = '12px'; break; case 'md': padding = '12px 24px'; fontSize = '14px'; break; case 'lg': padding = '14px 32px'; fontSize = '16px'; break; case 'xl': padding = '18px 40px'; fontSize = '18px'; break; case 'block': padding = '18px 40px'; display = 'block'; width = '100%'; margin = '10px 6px'; fontSize = '20px'; break; default: break; } let style = { display : display, width: width, backgroundColor : backgroundColor, color : color, fontWeight: 'bold', outline: 'none', border: 'none', margin: margin, padding: padding, fontSize: fontSize, borderRadius: '6px', cursor: 'pointer', } return ( <button style={style} onClick={() => onClick()}>{text}</button> ) } export default CustomButton
JavaScript
볡사

App.js

import './App.css'; import Button from './components/Button'; import CustomButton from './components/CustomButton'; function App() { const onClick = () => { alert('Clicked!') } return ( <div className='container'> <h1>inline style</h1> <hr/> <h2>κΈ°λ³Έ λ²„νŠΌ</h2> <Button /> <hr/> <h2>μ»€μŠ€ν…€ λ²„νŠΌ</h2> <h2>xs</h2> <CustomButton text='primary' btnStyle='primary' btnSize='xs' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='xs' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='xs' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='xs' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='xs' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='xs' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='xs' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='xs' onClick={onClick} /> <h2>sm</h2> <CustomButton text='primary' btnStyle='primary' btnSize='sm' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='sm' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='sm' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='sm' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='sm' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='sm' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='sm' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='sm' onClick={onClick} /> <h2>md</h2> <CustomButton text='primary' btnStyle='primary' btnSize='md' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='md' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='md' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='md' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='md' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='md' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='md' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='md' onClick={onClick} /> <h2>lg</h2> <CustomButton text='primary' btnStyle='primary' btnSize='lg' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='lg' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='lg' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='lg' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='lg' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='lg' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='lg' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='lg' onClick={onClick} /> <h2>xl</h2> <CustomButton text='primary' btnStyle='primary' btnSize='xl' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='xl' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='xl' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='xl' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='xl' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='xl' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='xl' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='xl' onClick={onClick} /> <h2>block</h2> <CustomButton text='primary' btnStyle='primary' btnSize='block' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='block' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='block' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='block' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='block' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='block' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='block' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='block' onClick={onClick} /> </div> ); } export default App;
JavaScript
볡사

import CSS

외뢀에 μŠ€νƒ€μΌ μ‹œνŠΈ(CSS) νŒŒμΌμ„ μƒμ„±ν•˜κ³ , 이λ₯Ό λΆˆλŸ¬μ™€μ„œ μŠ€νƒ€μΌμ„ μ μš©ν•˜λŠ” 방법

문법

import './파일λͺ….css';
JavaScript
볡사
<μ»΄ν¬λ„ŒνŠΈ id="κ³ μœ ν•œμ•„μ΄λ””" className="ν΄λž˜μŠ€μ΄λ¦„" />
JavaScript
볡사

μ˜ˆμ‹œμ½”λ“œ

import CSS 을 μ μš©ν•˜μ—¬ λ²„νŠΌμ„ λ§Œλ“€μ–΄ λ΄…λ‹ˆλ‹€.

κΈ°λ³Έ λ²„νŠΌ

Button.jsx
import React from 'react'; import './Button.css'; const Button = ({ id }) => { return ( <button id={id} className='btn'>Button</button> ) } export default Button
JavaScript
볡사
Button.css
.btn { background-color: royalblue; color: white; font-weight: bold; outline: none; border: none; margin: 0 2px; padding: 10px 24px; border-radius: 6px; cursor: pointer; } #btn { background-color: red; color: white; font-weight: bold; outline: none; border: none; margin: 0 2px; padding: 10px 24px; border-radius: 6px; cursor: pointer; }
CSS
볡사

μ»€μŠ€ν…€ λ²„νŠΌ

CustomButton.jsx
import React from 'react' import './CustomButton.css' const CustomButton = ({ text, btnStyle, btnSize, onClick }) => { return ( <button className={`btn ${btnStyle} ${btnSize}`} onClick={() => onClick()}>{text}</button> ) } export default CustomButton
JavaScript
볡사
CustomButton.css
.btn { display: inline-block; width: auto; background-color: black; color: white; font-weight: bold; outline: none; border: none; margin: 0 4px; padding: 12px 24px; font-size: 14px; border-radius: 6px; cursor: pointer; } .btn:hover { box-shadow: 2px 2px 2px rgba(0,0,0,0.5); filter: brightness(80%) contrast(100%) saturate(100%); } .btn:active { box-shadow: none; } .btn.xs { padding: 2px 8px; font-size: 10px; } .btn.sm { padding: 6px 12px; font-size: 12px; } .btn.md { padding: 12px 24px; font-size: 14px; } .btn.lg { padding: 14px 32px; font-size: 16px; } .btn.xl { padding: 18px 40px; font-size: 18px; } .btn.block { padding: 18px 40px; display: block; width: 100%; margin: 10px 6px; font-size: 20px; } /* Color variations */ .btn.primary { background-color: #007BFF; } .btn.secondary { background-color: #6C757D; } .btn.success { background-color: #28A745; } .btn.danger { background-color: #DC3545; } .btn.warning { background-color: #FFC107; color: black; } .btn.info { background-color: #17A2B8; } .btn.light { background-color: #F8F9FA; color: black; } .btn.dark { background-color: #343A40; }
CSS
볡사

App.js

import './App.css'; import Button from './components/Button/Button'; import CustomButton from './components/CustomButton/CustomButton'; function App() { const onClick = () => { alert('Clicked!') } return ( <div className='container'> <h1>import css</h1> <hr/> <h2>κΈ°λ³Έ λ²„νŠΌ</h2> <Button /> <Button id='btn' /> <hr/> <h2>μ»€μŠ€ν…€ λ²„νŠΌ</h2> <h2>xs</h2> <CustomButton text='primary' btnStyle='primary' btnSize='xs' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='xs' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='xs' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='xs' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='xs' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='xs' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='xs' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='xs' onClick={onClick} /> <h2>sm</h2> <CustomButton text='primary' btnStyle='primary' btnSize='sm' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='sm' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='sm' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='sm' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='sm' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='sm' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='sm' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='sm' onClick={onClick} /> <h2>md</h2> <CustomButton text='primary' btnStyle='primary' btnSize='md' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='md' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='md' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='md' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='md' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='md' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='md' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='md' onClick={onClick} /> <h2>lg</h2> <CustomButton text='primary' btnStyle='primary' btnSize='lg' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='lg' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='lg' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='lg' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='lg' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='lg' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='lg' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='lg' onClick={onClick} /> <h2>xl</h2> <CustomButton text='primary' btnStyle='primary' btnSize='xl' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='xl' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='xl' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='xl' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='xl' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='xl' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='xl' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='xl' onClick={onClick} /> <h2>block</h2> <CustomButton text='primary' btnStyle='primary' btnSize='block' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='block' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='block' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='block' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='block' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='block' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='block' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='block' onClick={onClick} /> </div> ); } export default App;
JavaScript
볡사

μ£Όμ˜μ‚¬ν•­

μ„œλ‘œ λ‹€λ₯Έ μ»΄ν¬λ„ŒνŠΈμ—μ„œ κ°œλ³„μ μœΌλ‘œ λ‹€λ₯Έ CSS λ₯Ό import ν•˜λ”λΌλ„, μŠ€νƒ€μΌμ΄ 전역에 μ μš©λ©λ‹ˆλ‹€.
κ°œλ³„μ μΈ μ»΄ν¬λ„ŒνŠΈμ—μ„œ, 각각의 CSS import ν•˜λ”λΌκ³  μ‹€μ œλ‘œλŠ” ν•˜λ‚˜μ˜ html 에 각각의 <style> 에 λžœλ”λ§ λ©λ‹ˆλ‹€.
λ”°λΌμ„œ, μ»΄ν¬λ„ŒνŠΈλ³„λ‘œ import ν•œ CSS 파일이 κ²°κ³Όμ μœΌλ‘œλŠ” ν•˜λ‚˜λ‘œ ν•©μ³μ €μ„œ μ μš©λ˜λŠ” 것을 λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.
Button.jsx μ»΄ν¬λ„ŒνŠΈμ—μ„œ 같은 경둜의 Button.css λ₯Ό ν¬ν•¨ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.
CustomButton.jsx μ»΄ν¬λ„ŒνŠΈμ—μ„œ 같은 경둜의 CustomButton.css λ₯Ό ν¬ν•¨ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.
μ‹€μ œλ‘œ, 각 μ»΄ν¬λ„ŒνŠΈμ—μ„œ import ν•œ CSS λŠ” index.html νŒŒμΌμ— <style> νƒœκ·Έλ‘œ μ—¬λŸ¬ κ°œκ°€ λžœλ”λ§ λ©λ‹ˆλ‹€.
β€’
<style> : index.css
β€’
<style> : App.css
β€’
<style> : Button.css
β€’
<style> : CustomButton.css
μ΄λ ‡κ²Œ κ°œλ³„λ‘œ import ν•œ css 파일일 ν•˜λ‚˜μ˜ html 둜 μ „μ—­μ—μ„œ μ„€μ •λ˜λ‹€λ³΄λ‹ˆ, λ‹€λ₯Έ css νŒŒμΌμ—μ„œ μ§€μ •ν•œ μ„ νƒμžκ°€ κ°™μœΌλ©΄, μŠ€νƒ€μΌλ§ μ€‘λ³΅λ˜λŠ” ν˜„μƒμ΄ λ°œμƒν•©λ‹ˆλ‹€.
μ—¬κΈ°μ„œλŠ”, Button.css μ—μ„œλ„ .btn μ„ νƒμžλ₯Ό μ§€μ •ν•˜κ³ , CustomButton.css μ—μ„œλŠ” .btn μ„ νƒμžλ₯Ό μ§€μ •ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. μ΄λŸ¬ν•œ λ°©μ‹μœΌλ‘œ 지정할 λ•ŒλŠ”, λ‹€λ₯Έ μ»΄ν¬λ„ŒνŠΈμ˜ CSS 라면 같은 μ΄λ¦„μ˜ μ„ νƒμžμ€ μ‚¬μš©ν•˜μ§€ μ•ŠλŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.
그런데, μ„ νƒμžκ°€ μ€‘λ³΅λ˜λ”λΌλ„, 각 μ»΄ν¬λ„ŒνŠΈ λ³„λ‘œ CSS λ₯Ό μ μš©μ‹œμΌœ λ”°λ‘œ 관리할 μˆ˜λŠ” μ—†μ„κΉŒμš”?
이에 λŒ€ν•œ 해결방법이 λ°”λ‘œ CSS Module μž…λ‹ˆλ‹€.

import CSS Module

CSSλ₯Ό λͺ¨λ“ˆν™”ν•˜μ—¬ μ»΄ν¬λ„ŒνŠΈ μˆ˜μ€€μ—μ„œ μŠ€νƒ€μΌμ„ 관리할 수 μžˆλ„λ‘ λ„μ™€μ£ΌλŠ” 기술
CSS Module은 클래슀 μ„ νƒμžμ— λŒ€ν•œ 지역 λ²”μœ„λ‘œμ˜ μŠ€νƒ€μΌμ„ μ œκ³΅ν•©λ‹ˆλ‹€.
μ»΄ν¬λ„ŒνŠΈλ³„ 클래슀 μ„ νƒμžλ₯Ό μ œκ³΅ν•˜κΈ° μœ„ν•¨
클래슀 μ„ νƒμžλ₯Ό μ€‘μ‹¬μœΌλ‘œ μŠ€μ½”ν”„κ°€ μ§€μ •λ©λ‹ˆλ‹€. idλ‚˜ νƒœκ·Έ μ„ νƒμžλ₯Ό μ‚¬μš©ν•˜λ €λ©΄ λ‹€λ₯Έ μŠ€νƒ€μΌ 방법을 μ‚¬μš©ν•˜κ±°λ‚˜ μ „μ—­ μŠ€νƒ€μΌλ‘œ μ •μ˜ν•΄μ•Ό ν•©λ‹ˆλ‹€.
1.
κ³ μœ ν•œ μŠ€μ½”ν”„: CSS Modules은 클래슀 이름을 μ§€μ—­μ μœΌλ‘œ μŠ€μ½”ν”„ν™”ν•˜μ—¬ μ „μ—­ λ„€μž„μŠ€νŽ˜μ΄μŠ€μ—μ„œμ˜ μΆ©λŒμ„ λ°©μ§€ν•©λ‹ˆλ‹€. 각 λͺ¨λ“ˆμ€ 자체 κ³ μœ ν•œ 클래슀 이름을 μƒμ„±ν•©λ‹ˆλ‹€.
2.
클래슀 이름 λ³€ν™˜: CSS Modules은 클래슀 이름을 μžλ™μœΌλ‘œ λ³€ν™˜ν•˜μ—¬ μ»΄ν¬λ„ŒνŠΈμ˜ 클래슀 이름과 μΆ©λŒν•˜μ§€ μ•Šλ„λ‘ ν•©λ‹ˆλ‹€. 일반적으둜 "camelCase"둜 μž‘μ„±λœ 클래슀 이름은 ν•˜μ΄ν”ˆμ„ ν¬ν•¨ν•˜λŠ” "kebab-case"둜 λ³€ν™˜λ©λ‹ˆλ‹€.
3.
클래슀 이름 μ‚¬μš©: JavaScript μ½”λ“œμ—μ„œ CSS Modules을 μ‚¬μš©ν•  λ•ŒλŠ” ν•΄λ‹Ή λͺ¨λ“ˆμ—μ„œ 내보낸 클래슀λ₯Ό λΆˆλŸ¬μ™€μ„œ μ‚¬μš©ν•©λ‹ˆλ‹€. 이λ₯Ό 톡해 μŠ€νƒ€μΌμ„ μ μš©ν•˜κ³  관리할 수 μžˆμŠ΅λ‹ˆλ‹€.
μ‹€μ œλ‘œ style νƒœκ·Έμ— λžœλ”λ§ 될 λ•Œ, 각 CSS λͺ¨λ“ˆμ—μ„œ μ§€μ •ν•œ .btn μ„ νƒμžκ°€ κ³ μœ ν•œ μ΄λ¦„μœΌλ‘œ μ§€μ •λ˜μ–΄μ„œ λžœλ”λ§ λ©λ‹ˆλ‹€.

문법

{파일λͺ…}.module.css ν˜•μ‹μœΌλ‘œ 파일 생성
import styles from './파일λͺ….module.css';
JavaScript
볡사
<μ»΄ν¬λ„ŒνŠΈ className={styles.ν΄λž˜μŠ€μ†μ„±} />
JavaScript
볡사

κ΄€λ‘€

CSS module 의 클래슀 μ„ νƒμžλŠ” 카멜 μΌ€μ΄μŠ€λ‘œ μ“°λŠ” 것이 κ΄€λ‘€ (μΌ€λ°₯ μΌ€μ΄μŠ€λ„ κ°€λŠ₯)
μΉ΄λ©œμΌ€μ΄μŠ€ : .formInput ➑ { styles.formInput } μΌ€λ°₯μΌ€μ΄μŠ€ : .form-input ➑ { styles['form-input']}
JavaScript
볡사

κΈ°λ³Έ λ²„νŠΌ

Button.jsx
import React from 'react'; // import './Button.css'; import styles from './Button.module.css'; const Button = ({ id }) => { return ( <button id={id} className={styles.btn}>Button</button> ) } export default Button
JavaScript
볡사
Button.module.css
.btn { background-color: royalblue; color: white; font-weight: bold; outline: none; border: none; margin: 0 2px; padding: 10px 24px; border-radius: 6px; cursor: pointer; } #btn { background-color: red; color: white; font-weight: bold; outline: none; border: none; margin: 0 2px; padding: 10px 24px; border-radius: 6px; cursor: pointer; }
CSS
볡사

μ»€μŠ€ν…€ λ²„νŠΌ

CustomButton.jsx
import React from 'react' // import './CustomButton.css' import styles from './CustomButton.module.css'; const CustomButton = ({ text, btnStyle, btnSize, onClick }) => { return ( <button className={`${styles.btn} ${styles[btnStyle]} ${styles[btnSize]}`} onClick={() => onClick()}>{text}</button> ) } export default CustomButton
JavaScript
볡사
CustomButton.module.css
.btn { display: inline-block; width: auto; background-color: black; color: white; font-weight: bold; outline: none; border: none; margin: 0 4px; padding: 12px 24px; font-size: 14px; border-radius: 6px; cursor: pointer; } .btn:hover { box-shadow: 2px 2px 2px rgba(0,0,0,0.5); filter: brightness(80%) contrast(100%) saturate(100%); } .btn:active { box-shadow: none; } .btn.xs { padding: 2px 8px; font-size: 10px; } .btn.sm { padding: 6px 12px; font-size: 12px; } .btn.md { padding: 12px 24px; font-size: 14px; } .btn.lg { padding: 14px 32px; font-size: 16px; } .btn.xl { padding: 18px 40px; font-size: 18px; } .btn.block { padding: 18px 40px; display: block; width: 100%; margin: 10px 6px; font-size: 20px; } /* Color variations */ .btn.primary { background-color: #007BFF; } .btn.secondary { background-color: #6C757D; } .btn.success { background-color: #28A745; } .btn.danger { background-color: #DC3545; } .btn.warning { background-color: #FFC107; color: black; } .btn.info { background-color: #17A2B8; } .btn.light { background-color: #F8F9FA; color: black; } .btn.dark { background-color: #343A40; }
CSS
볡사

App.js

import './App.css'; import Button from './components/Button/Button'; import CustomButton from './components/CustomButton/CustomButton'; function App() { const onClick = () => { alert('Clicked!') } return ( <div className='container'> <h1>import css</h1> <hr/> <h2>κΈ°λ³Έ λ²„νŠΌ</h2> <Button /> <Button id='btn' /> <hr/> <h2>μ»€μŠ€ν…€ λ²„νŠΌ</h2> <h2>xs</h2> <CustomButton text='primary' btnStyle='primary' btnSize='xs' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='xs' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='xs' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='xs' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='xs' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='xs' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='xs' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='xs' onClick={onClick} /> <h2>sm</h2> <CustomButton text='primary' btnStyle='primary' btnSize='sm' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='sm' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='sm' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='sm' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='sm' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='sm' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='sm' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='sm' onClick={onClick} /> <h2>md</h2> <CustomButton text='primary' btnStyle='primary' btnSize='md' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='md' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='md' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='md' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='md' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='md' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='md' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='md' onClick={onClick} /> <h2>lg</h2> <CustomButton text='primary' btnStyle='primary' btnSize='lg' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='lg' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='lg' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='lg' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='lg' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='lg' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='lg' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='lg' onClick={onClick} /> <h2>xl</h2> <CustomButton text='primary' btnStyle='primary' btnSize='xl' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='xl' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='xl' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='xl' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='xl' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='xl' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='xl' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='xl' onClick={onClick} /> <h2>block</h2> <CustomButton text='primary' btnStyle='primary' btnSize='block' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='block' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='block' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='block' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='block' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='block' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='block' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='block' onClick={onClick} /> </div> ); } export default App;
JavaScript
볡사

styled-components

React μ—μ„œ μŠ€νƒ€μΌμ΄ 적용된 μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‚¬μš©ν•  수 있게 ν•΄μ£ΌλŠ” 라이브러리

μ„€μΉ˜

npm install styled-components
Shell
볡사

문법

import styled from 'styled-components'; const μ»΄ν¬λ„ŒνŠΈλͺ… = styled.νƒœκ·Έλͺ…` 속성1 : κ°’1; 속성2 : κ°’2; `;
JavaScript
볡사
1.
styled-components 라이브러리λ₯Ό import ν•©λ‹ˆλ‹€.
2.
styled.νƒœκ·Έλͺ… - μ‚¬μš©ν•  νƒœκ·Έ 이름을 μ§€μ •ν•©λ‹ˆλ‹€.
3.
` ` ν…œν”Œλ¦Ώ λ¦¬ν„°λŸ΄μ„ μ‚¬μš©ν•˜μ—¬ μŠ€νƒ€μΌ 속성을 μ§€μ •ν•©λ‹ˆλ‹€.
a.
속성은 μΌ€λ°₯ μΌ€μ΄μŠ€(’style-property’)둜 μ§€μ •ν•©λ‹ˆλ‹€.
b.
값은 λ”°μ˜΄ν‘œ(’’) 없이 μ§€μ •ν•©λ‹ˆλ‹€.

μ˜ˆμ‹œ μ½”λ“œ

styled-components 라이브러리λ₯Ό μ‚¬μš©ν•˜μ—¬ λ²„νŠΌμ„ λ§Œλ“€μ–΄ λ΄…λ‹ˆλ‹€.

κΈ°λ³Έ λ²„νŠΌ

Button.jsx
import React from 'react' import styled from 'styled-components'; // λ²„νŠΌ μŠ€νƒ€μΌ const buttonStyles = ` font-weight: bold; outline: none; border: none; margin: 0 2px; padding: 10px 24px; border-radius: 6px; cursor: pointer; `; // 일반 λ²„νŠΌ μŠ€νƒ€μΌ const standard = ` background-color: royalblue; color: white; `; // IDκ°€ "btn"인 λ²„νŠΌ μŠ€νƒ€μΌ const unique = ` background-color: red; color: white; `; // μŠ€νƒ€μΌλ“œ μ»΄ν¬λ„ŒνŠΈ 생성 const StyledButton = styled.button` ${buttonStyles} ${props => props.id === 'btn' ? unique : standard} `; // Button μ»΄ν¬λ„ŒνŠΈ const Button = ({ id }) => { return <StyledButton id={id}>Button</StyledButton>; } export default Button;
JavaScript
볡사

μ»€μŠ€ν…€ λ²„νŠΌ

CustomButton.jsx
import React from 'react'; import styled from 'styled-components'; const StyledButton = styled.button` display: ${props => props.display || 'inline-block'}; width: ${props => props.width || 'auto'}; background-color: ${props => props.backgroundColor || 'black'}; color: ${props => props.color || 'white'}; font-weight: bold; outline: none; border: none; margin: ${props => props.margin || '0 4px'}; padding: ${props => props.padding || '12px 24px'}; font-size: ${props => props.fontSize || '14px'}; border-radius: 6px; cursor: pointer; `; const CustomButton = ({ text, btnStyle, btnSize, onClick }) => { let backgroundColor = 'black'; let color = 'white'; let padding = '12px 24px'; let display = 'inline-block'; let fontSize = '14px'; let width = 'auto'; let margin = '0 4px'; switch (btnStyle) { case 'primary': backgroundColor = '#007BFF'; break; case 'secondary': backgroundColor = '#6C757D'; break; case 'success': backgroundColor = '#28A745';break; case 'danger': backgroundColor = '#DC3545'; break; case 'warning': backgroundColor = '#FFC107'; color = 'black'; break; case 'info': backgroundColor = '#17A2B8'; break; case 'light': backgroundColor = '#F8F9FA'; color = 'black'; break; case 'dark': backgroundColor = '#343A40'; break; default: break; } switch (btnSize) { case 'xs': padding = '2px 8px'; fontSize = '10px'; break; case 'sm': padding = '6px 12px'; fontSize = '12px'; break; case 'md': padding = '12px 24px'; fontSize = '14px'; break; case 'lg': padding = '14px 32px'; fontSize = '16px'; break; case 'xl': padding = '18px 40px'; fontSize = '18px'; break; case 'block': padding = '18px 40px'; display = 'block'; width = '100%'; margin = '10px 6px'; fontSize = '20px'; break; default: break; } return ( <StyledButton backgroundColor={backgroundColor} color={color} padding={padding} display={display} fontSize={fontSize} width={width} margin={margin} onClick={onClick} > {text} </StyledButton> ); }; export default CustomButton;
JavaScript
볡사

App.js

import './App.css'; import Button from './components/Button'; import CustomButton from './components/CustomButton'; function App() { const onClick = () => { alert('Clicked!') } return ( <div className='container'> <h1>styled components</h1> <hr/> <h2>κΈ°λ³Έ λ²„νŠΌ</h2> <Button /> <Button id='btn' /> <hr/> <h2>μ»€μŠ€ν…€ λ²„νŠΌ</h2> <h2>xs</h2> <CustomButton text='primary' btnStyle='primary' btnSize='xs' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='xs' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='xs' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='xs' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='xs' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='xs' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='xs' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='xs' onClick={onClick} /> <h2>sm</h2> <CustomButton text='primary' btnStyle='primary' btnSize='sm' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='sm' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='sm' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='sm' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='sm' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='sm' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='sm' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='sm' onClick={onClick} /> <h2>md</h2> <CustomButton text='primary' btnStyle='primary' btnSize='md' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='md' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='md' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='md' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='md' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='md' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='md' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='md' onClick={onClick} /> <h2>lg</h2> <CustomButton text='primary' btnStyle='primary' btnSize='lg' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='lg' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='lg' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='lg' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='lg' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='lg' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='lg' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='lg' onClick={onClick} /> <h2>xl</h2> <CustomButton text='primary' btnStyle='primary' btnSize='xl' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='xl' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='xl' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='xl' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='xl' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='xl' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='xl' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='xl' onClick={onClick} /> <h2>block</h2> <CustomButton text='primary' btnStyle='primary' btnSize='block' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='block' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='block' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='block' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='block' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='block' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='block' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='block' onClick={onClick} /> </div> ); } export default App;
JavaScript
볡사

BootStrap

λ°˜μ‘ν˜• μ›Ή λ””μžμΈ 및 κ°œλ°œμ„ μœ„ν•œ HTML, CSS 및 JavaScript ν”„λ ˆμž„μ›Œν¬

문법

<μ»΄ν¬λ„ŒνŠΈ className="λ―Έλ¦¬μ •μ˜λœ ν΄λž˜μŠ€μ†μ„±" />
JavaScript
볡사

μ˜ˆμ‹œ

<button className="btn btn-primary">Button</button>
JavaScript
볡사

μ˜ˆμ‹œ μ½”λ“œ

bootstrap 을 μ‚¬μš©ν•˜μ—¬ λ²„νŠΌμ„ λ§Œλ“€μ–΄ λ΄…λ‹ˆλ‹€.

κΈ°λ³Έ λ²„νŠΌ

Button.jsx
import React from 'react'; import 'bootstrap/dist/css/bootstrap.min.css'; // βœ… Bootstrap CSSλ₯Ό μΆ”κ°€ const Button = () => { return ( <button className="btn btn-primary">Button</button> ); } export default Button;
JavaScript
볡사

μ»€μŠ€ν…€ λ²„νŠΌ

CustomButton.jsx
import React from 'react'; import 'bootstrap/dist/css/bootstrap.min.css'; // βœ… Bootstrap CSSλ₯Ό μΆ”κ°€ const CustomButton = ({ text, btnStyle, btnSize, onClick }) => { let buttonClasses = 'btn'; switch (btnStyle) { case 'primary': buttonClasses += ' btn-primary'; break; case 'secondary': buttonClasses += ' btn-secondary'; break; case 'success': buttonClasses += ' btn-success'; break; case 'danger': buttonClasses += ' btn-danger'; break; case 'warning': buttonClasses += ' btn-warning'; break; case 'info': buttonClasses += ' btn-info'; break; case 'light': buttonClasses += ' btn-light'; break; case 'dark': buttonClasses += ' btn-dark'; break; default: break; } switch (btnSize) { case 'xs': buttonClasses += ' btn-xs'; break; case 'sm': buttonClasses += ' btn-sm'; break; case 'md': buttonClasses += ' btn-md'; break; case 'lg': buttonClasses += ' btn-lg'; break; case 'xl': buttonClasses += ' '; break; // bootstrap5 버전 btn-xl ❌ deprecated case 'block': buttonClasses += ' '; break; // bootstrap5 버전 btn-block ❌ deprecated default: break; } return ( <button className={buttonClasses} onClick={() => onClick()}>{text}</button> ); } export default CustomButton;
JavaScript
볡사

App.js

import './App.css'; import Button from './components/Button'; import CustomButton from './components/CustomButton'; function App() { const onClick = () => { alert('Clicked!') } return ( <div className='container'> <h1>bootstrap</h1> <hr/> <h2>κΈ°λ³Έ λ²„νŠΌ</h2> <Button /> <hr/> <h2>μ»€μŠ€ν…€ λ²„νŠΌ</h2> <h2>xs</h2> <div className='d-flex justify-content-between align-items-center'> <CustomButton text='primary' btnStyle='primary' btnSize='xs' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='xs' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='xs' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='xs' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='xs' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='xs' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='xs' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='xs' onClick={onClick} /> </div> <h2>sm</h2> <div className='d-flex justify-content-between align-items-center'> <CustomButton text='primary' btnStyle='primary' btnSize='sm' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='sm' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='sm' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='sm' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='sm' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='sm' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='sm' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='sm' onClick={onClick} /> </div> <h2>md</h2> <div className='d-flex justify-content-between align-items-center'> <CustomButton text='primary' btnStyle='primary' btnSize='md' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='md' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='md' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='md' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='md' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='md' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='md' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='md' onClick={onClick} /> </div> <h2>lg</h2> <div className='d-flex justify-content-between align-items-center'> <CustomButton text='primary' btnStyle='primary' btnSize='lg' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='lg' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='lg' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='lg' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='lg' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='lg' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='lg' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='lg' onClick={onClick} /> </div> <h2>xl</h2> <div class="d-grid gap-2 col-6 mx-auto"> <CustomButton text='primary' btnStyle='primary' btnSize='xl' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='xl' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='xl' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='xl' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='xl' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='xl' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='xl' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='xl' onClick={onClick} /> </div> <h2>block</h2> <div class="d-grid gap-2"> <CustomButton text='primary' btnStyle='primary' btnSize='block' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='block' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='block' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='block' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='block' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='block' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='block' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='block' onClick={onClick} /> </div> </div> ); } export default App;
JavaScript
볡사

React-BootStrap

Bootstrap을 React μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ μ‚¬μš©ν•  수 μžˆλ„λ‘ React μ»΄ν¬λ„ŒνŠΈλ‘œ 감싼 라이브러리
React와 Bootstrap을 μ‘°ν•©ν•˜μ—¬ 효과적으둜 μ‚¬μš©ν•  수 μžˆλ„λ‘ ν•΄μ£Όλ©°, React의 μ»΄ν¬λ„ŒνŠΈ 기반 μ•„ν‚€ν…μ²˜λ₯Ό ν™œμš©ν•˜μ—¬ Bootstrap을 λ”μš± μœ μ—°ν•˜κ²Œ μ‚¬μš©ν•  수 있게 ν•©λ‹ˆλ‹€.

μ„€μΉ˜

npm install react-bootstrap bootstrap
Shell
볡사

문법

// 방법 1️⃣ import μ»΄ν¬λ„ŒνŠΈ from 'react-bootstrap/μ»΄ν¬λ„ŒνŠΈ'; // 방법 2️⃣ import { μ»΄ν¬λ„ŒνŠΈ} from 'react-bootstrap'; <μ»΄ν¬λ„ŒνŠΈ μ˜΅μ…˜="κ°’" μ˜΅μ…˜2="κ°’" />
JavaScript
볡사

μ˜ˆμ‹œ

import Button from 'react-bootstrap/Button'; <Button variant='primary' onClick={onClick}>Button</Button>
JavaScript
볡사

μ˜ˆμ‹œ μ½”λ“œ

react-bootstrap 을 μ‚¬μš©ν•˜μ—¬ λ²„νŠΌμ„ λ§Œλ“€μ–΄ λ΄…λ‹ˆλ‹€.

κΈ°λ³Έ λ²„νŠΌ

ReactButton.jsx
import React from 'react'; import Button from 'react-bootstrap/Button'; const ReactButton = () => { return ( <Button variant='primary'>Button</Button> ); } export default ReactButton;
JavaScript
볡사

μ»€μŠ€ν…€ λ²„νŠΌ

CustomButton.jsx
import 'bootstrap/dist/css/bootstrap.min.css'; import Button from 'react-bootstrap/Button'; const CustomButton = ({ text, btnStyle, btnSize, onClick }) => { let variant = 'primary'; switch (btnStyle) { case 'secondary': variant = 'secondary'; break; case 'success': variant = 'success'; break; case 'danger': variant = 'danger'; break; case 'warning': variant = 'warning'; break; case 'info': variant = 'info'; break; case 'light': variant = 'light'; break; case 'dark': variant = 'dark'; break; default: break; } let size = ''; switch (btnSize) { case 'xs': size = 'sm'; break; case 'sm': size = 'sm'; break; case 'md': size = ''; break; case 'lg': size = 'lg'; break; default: break; } return ( <Button variant={variant} size={size} onClick={() => onClick()}>{text}</Button> ); } export default CustomButton;
JavaScript
볡사

App.js

import './App.css'; import CustomButton from './components/CustomButton'; import ReactButton from './components/ReactButton'; function App() { const onClick = () => { alert('Clicked!') } return ( <div className='container'> <h1>react-bootstrap</h1> <hr/> <h2>κΈ°λ³Έ λ²„νŠΌ</h2> <ReactButton /> <hr/> <h2>μ»€μŠ€ν…€ λ²„νŠΌ</h2> <h2>xs</h2> <div className='d-flex justify-content-between align-items-center'> <CustomButton text='primary' btnStyle='primary' btnSize='xs' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='xs' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='xs' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='xs' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='xs' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='xs' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='xs' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='xs' onClick={onClick} /> </div> <h2>sm</h2> <div className='d-flex justify-content-between align-items-center'> <CustomButton text='primary' btnStyle='primary' btnSize='sm' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='sm' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='sm' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='sm' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='sm' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='sm' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='sm' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='sm' onClick={onClick} /> </div> <h2>md</h2> <div className='d-flex justify-content-between align-items-center'> <CustomButton text='primary' btnStyle='primary' btnSize='md' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='md' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='md' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='md' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='md' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='md' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='md' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='md' onClick={onClick} /> </div> <h2>lg</h2> <div className='d-flex justify-content-between align-items-center'> <CustomButton text='primary' btnStyle='primary' btnSize='lg' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='lg' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='lg' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='lg' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='lg' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='lg' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='lg' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='lg' onClick={onClick} /> </div> <h2>xl</h2> <div class="d-grid gap-2 col-6 mx-auto"> <CustomButton text='primary' btnStyle='primary' btnSize='xl' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='xl' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='xl' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='xl' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='xl' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='xl' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='xl' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='xl' onClick={onClick} /> </div> <h2>block</h2> <div class="d-grid gap-2"> <CustomButton text='primary' btnStyle='primary' btnSize='block' onClick={onClick} /> <CustomButton text='secondary' btnStyle='secondary' btnSize='block' onClick={onClick} /> <CustomButton text='success' btnStyle='success' btnSize='block' onClick={onClick} /> <CustomButton text='danger' btnStyle='danger' btnSize='block' onClick={onClick} /> <CustomButton text='warning' btnStyle='warning' btnSize='block' onClick={onClick} /> <CustomButton text='info' btnStyle='info' btnSize='block' onClick={onClick} /> <CustomButton text='light' btnStyle='light' btnSize='block' onClick={onClick} /> <CustomButton text='dark' btnStyle='dark' btnSize='block' onClick={onClick} /> </div> </div> ); } export default App;
JavaScript
볡사