๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐Ÿฆ• ๊ณต๋ฃก์ด ๋˜์ž!

React...1 ๋ณธ๋ฌธ

Development/React

React...1

Kirok Kim 2022. 1. 11. 02:53
  • ์‹ฑ๊ธ€ ์›นํŽ˜์ด์ง€ ๋ฐฉ์‹์œผ๋กœ ๋ Œ๋”๋ง์„ ์ฃผ๊ธฐ์ ์œผ๋กœ ํ•ด์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ์ฆ‰๊ฐ์ ์œผ๋กœ ๋ฐ”๋€Œ์–ด์ง
  • html+js=jsx
  • html ์ปดํฌ๋„ŒํŠธ์™€ ๊ทธ์™€ ๊ด€๋ จ๋œ ํ•จ์ˆ˜๋ฅผ ๋ญ‰์ณ์„œ ์„ธํŠธ๋กœ ๋งŒ๋“œ๋Š”๋ฐ ๊ทธ๊ฒƒ์„ component
  • ์ด ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์žฌ์‚ฌ์šฉ์ด ์–ผ๋งˆ๋“ ์ง€ ๊ฐ€๋Šฅํ•ด์„œ ์ฝ”๋“œ๋Ÿ‰์„ ํš๊ธฐ์ ์œผ๋กœ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
  • component = ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ๊ฐ€๋Šฅ
Redux
  • ๋ชจ๋“  component๊ฐ€ props ์—†์ด state๋ฅผ ์ง์ ‘ ๊บผ๋‚ผ ์ˆ˜ ์žˆ์Œ
  • ์ƒํƒœ ๊ด€๋ฆฌ๊ฐ€ ์šฉ์ด(state ๊ด€๋ฆฌ ์šฉ์ด)
    • ์ปดํฌ๋„ŒํŠธ๊ฐ€ state๋ฅผ ๋ณ€๊ฒฝ์‹œํ‚ค๋‹ค ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋ฉด ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‹ค ํ™•์ธ์„ ํ•ด์•ผ๋จ์œผ๋กœ ์‹œ๊ฐ„์ด ๋งŽ์ด ๊ฑธ๋ฆผ ํ—Œ๋ฐ Redux๋ฅผ ์“ฐ๋ฉด ์ถ”์ ์ด ์šฉ์ดํ•จ
index.js
import {Provider} from 'react-redux';
import {createStore} from 'redux';

const ์ฒด์ค‘ =100;
function reducer(state=์ฒด์ค‘,action){//์ˆ˜์ • ๊บผ๋‚ด์“ฐ๋Š”๊ฑธ reducer
	if(action.type=='์ฆ๊ฐ€'){
		state++;
		return state
	}else if(action.type==='๊ฐ์†Œ'){
		state--;
	return state
	}else{
	return state
	}

}

let store=createStore(reducer)

ReactDOM.render(
	<React.StrictMode>
		<Provider store={store}>
			<App/>
		</Provider>
	</React.StrictMode>
	document.getElementById('root')
);

App.js
import {useSelector} from 'react-redux'
import {useDispatch,useSelector} from 'react-redux'
function App(){
	const ๊บผ๋‚ด์˜จ๊ฑฐ = useSelector((state)=>state);
	const dispatch = useDispatch()
	return(
		<div className="App">
			<p>๋‹˜์˜ ์ฒ˜์ฐธํ•œ ๋ชธ๋ฌด๊ฒŒ :{๊บผ๋‚ด์˜จ๊ฑฐ}</p>
			<button onClick={()=>{dispatch({type:'์ฆ๊ฐ€'})}}>๋”ํ•˜๊ธฐ</button>
		</div>
	);
}
export default App;
๋ฐ˜์‘ํ˜•

'Development > React' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์ •๋ฆฌ  (0) 2022.01.10
Comments