Home  >  Article  >  Web Front-end  >  Redux core explanation (code example)

Redux core explanation (code example)

不言
不言forward
2018-12-11 09:49:202003browse

This article brings you the core explanation (code example) of redux. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Concept

Redux is an architectural pattern that can be used in conjunction with react and vue.

Problems solved

Elegantly modify the shared data state to avoid chain changes in the state between parent and child components (it will be troublesome to change if there are many child components) and external changes There are unnecessary (difficult to eliminate) problems, so all changes must be made through one method (dispatch).

Redux core explanation (code example)

##Implementation steps

//state(数据)和action(控制修改)后的数据
function reducer (state, action) {
    /!* 初始化 state 和 switch case *!/
}

// 通过reducer获取state
// 执行action
// 监听数据变化
const store = createStore(reducer)

// 监听数据变化重新渲染页面
// 通过观察者模式监听数据变化,避免没有状态改变的频繁渲染
store.subscribe(() => renderApp(store.getState()))

// 首次渲染页面
renderApp(store.getState())

Example

const usersReducer = (state, action) => {
    if (!state) return [];
    switch (action.type) {
        case "ADD_USER":
            return [...state, action.user]
        case "DELETE_USER":
            return [...state.slice(0, action.index), ...state.slice(action.index + 1)]
        case "UPDATE_USER":
            let user = {
                ...state[action.index],
                ...action.user,
            }
            return [
                ...state.slice(0, action.index),
                user,
                ...state.slice(action.index + 1),
            ]
        default:
            return state
    }
}
//state(数据)和dispatch(控制修改)封装起来
function createStore (reducer) {
    let state = null
    const listeners = []
    const subscribe = (listener) => listeners.push(listener)
    const getState = () => state
    const dispatch = (action) => {
        state = reducer(state, action) // 覆盖原对象
        // console.log(listeners)
        listeners.forEach((listener) => {
            // console.log(listener)
            listener()

        })
    }
    dispatch({}) // 初始化 state
    return { getState, dispatch, subscribe }
}
const store = createStore(usersReducer);
console.log(store.getState());
//增
store.dispatch({
    type: 'ADD_USER',
    user: {
        username: 'Lucy',
        age: 12,
        gender: 'female'
    }
});
console.log(store.getState());
//改
store.dispatch({
    type: 'UPDATE_USER',
    index: 0,
    user: {
        username: 'Tomy',
        age: 12,
        gender: 'male'
    }
});
console.log(store.getState());
//删
store.dispatch({
    type: 'DELETE_USER',
    index: 0 // 删除特定下标用户
});
console.log(store.getState());

The above is the detailed content of Redux core explanation (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete