We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
redux里面主要有 action, reducer, dispatch, store, 他们三个的关系是
type
##Redux核心之间的关系
触发action -> reducer来处理。 这两者之间的关系,怎么来触发action呢?
Store这个对象提供了dispatch方法 -> 触发action。 dispatch方法接受action对象作为参数。
`store` ➡️ `dispatch` ➡️ `action` ⬅️ `reducer`
所以手写redux的关键就是
所以大致的逻辑如下:
function createStore(reducer){ let state; const listeners = []; const subscribe = (listener) => listeners.push(listener); const dispatch = (action) => { state = reducer(state, action) listeners.forEach((listener) => listener()) }; const getState = () => state; dispatch({}) return {dispatch, getState, subscribe} }
这个store的提供的方法主要是:
实际应用如下:
const store = createStore(reducer) let oldState = store.getState(); store.subscribe(() => { const newState = store.getState() renderApp(newState, oldState) oldState = newState; }) renderApp(store.getState()) store.dispatch({ type: '', text: 'xxx' })
这只是一个很简单的例子,没有像reducer里面的applyMiddleware,combineReducers,bindActionCreators这些方法。
applyMiddleware
combineReducers
bindActionCreators
完成及调用方法如下:
let appState = { title: { text: 'text title', color: 'red', }, content: { text: 'text content', color: 'blue' } } // reducer function stateChanger (state=appState, action) { switch (action.type) { case 'UPDATE_TITLE_TEXT': return { // 构建新的对象并且返回 ...state, title: { ...state.title, text: action.text } } // state.title.text = action.text break case 'UPDATE_TITLE_COLOR': return { // 构建新的对象并且返回 ...state, title: { ...state.title, color: action.color } } //state.title.color = action.color break default: return state break } } function dispatch (action) { switch (action.type) { case 'UPDATE_TITLE_TEXT': appState.title.text = action.text break case 'UPDATE_TITLE_COLOR': appState.title.color = action.color break default: break } } function renderApp (newAppState, oldAppState = {}) { if (newAppState === oldAppState) return console.log('render app...') renderTitle(newAppState.title, oldAppState.title ) renderContent(newAppState.content, oldAppState.content) } function renderTitle (newTitle, oldTitle = {}) { if (newTitle === oldTitle) return console.log('render title...') const titleDOM = document.getElementById('title') titleDOM.innerHTML = newTitle.text titleDOM.style.color = newTitle.color } function renderContent (newContent, oldContent) { if (newContent === oldContent) return console.log('render content...') const contentDOM = document.getElementById('content') contentDOM.innerHTML = newContent.text contentDOM.style.color = newContent.color } //renderApp(appState) // 首次渲染页面 //dispatch({ type: 'UPDATE_TITLE_TEXT', text: '《React.js 小书》' }) // 修改标题文本 //dispatch({ type: 'UPDATE_TITLE_COLOR', color: 'blue' }) // 修改标题颜色 //renderApp(appState) // 把新的数据渲染到页面上 function createStore(reducer){ let state; const listeners = []; const subscribe = (listener) => listeners.push(listener); const dispatch = (action) => { state = reducer(state, action) listeners.forEach((listener) => listener()) }; const getState = () => state; dispatch({}) return {dispatch, getState, subscribe} } const store = createStore(stateChanger) let oldState = store.getState(); store.subscribe(() => { const newState = store.getState() renderApp(newState, oldState) oldState = newState; }) //const newState = store.getState(); renderApp(store.getState()) store.dispatch({ type: 'UPDATE_TITLE_TEXT', text: 'tex1' }) store.dispatch({ type: 'UPDATE_TITLE_COLOR', color: 'blue' })
Redux: store, action, reducer
动手实现 Redux Redux 关系图解
The text was updated successfully, but these errors were encountered:
No branches or pull requests
redux里面主要有 action, reducer, dispatch, store, 他们三个的关系是
rudux的特点是:
Redux 三大核心
type
这个属性,reducer将根据这个属性值来对store进行相应的处理。除此之外的属性,就是进行这个操作需要的数据##Redux核心之间的关系
触发action -> reducer来处理。 这两者之间的关系,怎么来触发action呢?
Store这个对象提供了dispatch方法 -> 触发action。 dispatch方法接受action对象作为参数。
所以手写redux的关键就是
所以大致的逻辑如下:
这个store的提供的方法主要是:
实际应用如下:
这只是一个很简单的例子,没有像reducer里面的
applyMiddleware
,combineReducers
,bindActionCreators
这些方法。完成及调用方法如下:
真实的redux的流程:
Redux: store, action, reducer
动手实现 Redux
Redux 关系图解
The text was updated successfully, but these errors were encountered: