Giter VIP home page Giter VIP logo

redux_example's Introduction

Redux_example

Created with CodeSandbox

Store: createStore(reducer,initState)로 생성한다.

configureStore.js createStore(combineReducer(reducers),initState,composeWithDevTools())

---------Store----------- 설명: 1.combineReducer(reducers)

import { createStore, combineReducers } from "redux"; import { composeWithDevTools } from "redux-devtools-extension"; import reducers from "./reducers"; reducers/index.js: import loading from "./loading"; import user from "./user"; import collection from "./collection";

export default {
  collection,
  loading,
  user
};
reducers: 위 세 개의 리듀서 모음.
combineReducer로 조합해준다.

2.initState 초기 store에 들어가는 state

3..composeWithDevTools() devTool 사용을 위한 것.

store를 하위 컴포넌트에게 내려??줌

---------dispatch&reducer&subscribe&getState----------- action을 받아 reducer에게 전달. store.dispatch({type:"uniq string",payload: {변경될 state}})

dispatch는 store에 등록된 리듀서에게 전달. reducer는 type에 따라 store의 state값을 업데이트 요청함. store는 해당 state를 업데이트한다.

또 dispatch는 action이 들어올때마다 subscribe(listner)를 호출한다. {connect(mapStateToProps,DispatchToProps)(Component)나 useSelector Hook을 이용한다.} store.getState():현재 state값을 조회

// 스토어안에 들어있는 상태가 바뀔 때 마다 호출되는 listener 함수 const listener = () => { const state = store.getState(); console.log(state); };

const unsubscribe = store.subscribe(listener); // 구독을 해제하고 싶을 때는 unsubscribe() 를 호출


-------------PresentationComponet&ContainerComponet----------- PresentationComponet:일반적인 Component의 개념으로 redux와의 연관성이 없다. ContainerComponet:redux와 PresentationView의 중간역활로 mapStateToProps:Store의 State를 Props로 내려준다. mapDispatchToProps:dispatch를 Props로 내려준다. connect(mapStateToProps,DispatchToProps)(PresentationComponet) :mapStateToProps,DispatchToProps PresentationComponet를 연결

ParentComponent--props(1)-->ContainerComponet--props(2)-->PresentationComponet

ContainerComponet const mapStateToProps=(state,props)=>{ //state:store의 state, props: ContainerComponet 넘겨준 props(1) return{ PresentationComponet 뷰에 넘겨줄 Props(2)} } const mapDispatchToProps=(dispatch)=>{ const setAge=(id,age)=>dispatch({type:"SET_AGE",payload:{id,age}}) return{ //props(2)로 넘겨줄 dispatch된 함수들 이렇게 하면 subscribe할 필요 없음 setAge } } export default connect(mapStateToProps,mapDispatchToProps)(PresentationComponet)

Reducer "searchFilter" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined.

state를 리턴해주지 않았다.

redux_example's People

Contributors

settingrhino avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.