$ npm install --save redux
리덕스를 이용한다는 것은 결국 store를 만들어서 상태(state)를 바꾸는 것이 리덕스의 핵심이다.
적용순서
store가 생성되면 자동으로 state가 생긴다.
// store를 전역변수에 저장
var store = Redux.createStore(reducer);
// reducer를 이용해서 state값을 만들어줘야함
function reducer(state,action) {
// store를 처음 만들때 store에 있는 state에 초기값이 필요하다.
if(state === undefined) {
// state가 객체를 리턴한다면 리턴한 값이 state의 초기값이 된다.
return { color : 'yellow' }
}
}
다음과 같이 콘솔로 출력해본 결과 초기화해준 컬러가 출력되는 것이 확인된다.
console.log(store.getState()); // {color: "yellow"} 출력
getState()
로 store에 있는 state를 불러온 뒤 화면에 보여주도록 한다.function red() {
var state = store.getState();
document.querySelector('#red').innerHTML = `
<div
class="container"
id="component_red"
style="background-color:${state.color}"
>
<h1>red</h1>
<input
type="button"
value="fire"
onclick="
document.querySelector('#component_red').style.backgroundColor='red';
"
/>
</div>
`
}
red()
store.dispatch({type:'CHANGE_COLOR', color:'red'})
type
은 반드시 있어야 한다.dispatch
는 store를 생성할때 제공한 reducer 함수를 호출하도록 약속되어 있다.reducer는 이전의 state값과 action을 받아서 다음의 state값을 리턴해주는 함수
function reducer(state,action) {
// state 초기화
if(state === undefined) {
return { color : 'yellow' }
}
// state복제값
var newState;
// state 값 변경
if(action.type === 'CHANGE_COLOR' ) {
newState = Object.assign({},state,{ color:'red' });
}
return newState; // 복제한값 리턴
}
Object.assign({},{name :'mj'},{city:'seoul'})
// { name : 'mj', city: 'seoul' }
Object.assign()
사용{}
반드시 빈 객체
- reducer함수는 store의 state값을 변경해준다.
- 어떻게? action의 값과 이전의state값을 이용해서 새로운 state 값을 리턴해주면 리턴된 값이 새로운 state값이다.
- 리턴값은 원본을 바꾸는 것이 아니라 이전값을 복제한 결과를 리턴해야지 리덕스를 사용하는 여러가지 효율을 최대한으로 활용할 수 있다.
state값이 바뀌었으니 그에 따라 화면 UI가 변경되어야 한다. 그렇기 위해서는 render
를 호출해줘야 한다.
즉, state의 값이 바뀔때마다
render
가 통보받아서 그때마다 state값을 가져와 화면에 그려줄 수 있도록 한다.
store.subscribe(red) // UI를 변경해주는 red 함수
이렇게 한다면 state값이 바뀔때마다 red함수가 호출된다.
원본을 불변해야한다. 변경할때는 복제하고 사용해야한다.