React

 

 

샘플1.

redux-demo.js

// redux 라이브러리를 가져옵니다.
const redux = require('redux');

// 초기 상태를 설정합니다. 카운터는 0으로 시작합니다.
const initialState = {
    counter: 0
};

// 리듀서 함수를 정의합니다. 이 함수는 액션에 따라 상태를 변경합니다.
const counterReducer = (state = initialState, action) => {
    // 액션의 타입에 따라 다른 작업을 수행합니다.
    switch(action.type) {
        case 'increment':
            // 'increment' 액션의 경우, 카운터를 1 증가시킵니다.
            return {
                counter: state.counter + 1
            };
        default:
            // 알 수 없는 액션의 경우, 현재 상태를 그대로 반환합니다.
            return state;
    }
}

// 스토어를 생성합니다. 이 스토어는 애플리케이션의 상태를 관리합니다.
const store = redux.createStore(counterReducer);

// 구독자 함수를 정의합니다. 이 함수는 상태가 변경될 때마다 호출됩니다.
const counterSubscriber = () => {
    // 가장 최근의 상태를 가져옵니다.
    const latestState = store.getState();
    // 최근 상태를 콘솔에 출력합니다.
    console.log(latestState);
}

// 구독자 함수를 스토어에 등록합니다.
store.subscribe(counterSubscriber);

// 'increment' 액션을 디스패치합니다. 이로 인해 카운터가 1 증가합니다.
store.dispatch({type: 'increment'});

 

$ node redux-demo.js
{ counter: 1 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

about author

PHRASE

Level 60  라이트

정열은 천재와 같다. 정열에 의해 기적이 생기기 때문이다. -로망 롤랑

댓글 ( 0)

댓글 남기기

작성