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;
๋ฐ์ํ