Ya he visitado el
repositorio de la biblioteca
redux , pero de algún lado tuve la idea de profundizar en su implementación. Me gustaría compartir mi descubrimiento sorprendente o incluso decepcionante con la comunidad.
TL; DR: la lógica de núcleo redux cabe en 7 líneas de código JS.
Sobre redux en pocas palabras (traducción libre del encabezado en el github):
Redux es una biblioteca de gestión de estado para aplicaciones JavaScript.
Ayuda a escribir aplicaciones que se comporten de manera estable / predecible, funcionen en diferentes entornos (cliente / servidor / código nativo) y se prueben fácilmente.
Cloné el repositorio redux , abrí la carpeta de origen en el editor (ignorando
documentos ,
ejemplos , etc.) y
agarré la tecla Eliminar con las
tijeras :
- Se eliminaron todos los comentarios del código.
Cada método de biblioteca se documenta utilizando JSDoc en gran detalle.
- Se eliminó la validación de errores y el registro
En cada método, los parámetros de entrada se controlan estrictamente con la salida de comentarios detallados muy agradables a la consola
- Se eliminaron bindActionCreators , suscribirse , reemplazarReducer y métodos observables .
... porque pudo. Bueno, o porque era demasiado vago para escribir ejemplos para ellos. Pero sin casos de esquina, son aún menos interesantes que lo que se avecina.
Ahora echemos un vistazo a lo que queda
Redacción redux en 7 líneas
Toda la funcionalidad básica de redux cabe en un archivo pequeño, para el cual casi nadie creará un repositorio github :)
function createStore(reducer, initialState) {
let state = initialState
return {
dispatch: action => { state = reducer(state, action) },
getState: () => state,
}
}
. , ,
.
redux. 18 HeadHunter «redux» — , , 7 . — .
7 TodoApp. .
TodoApp redux.
function todosReducer(state, action) {
switch (action.type) {
case 'ADD_TODO':
return [
...state,
{
id: action.id,
text: action.text,
completed: false
}
]
case 'TOGGLE_TODO':
return state.map(todo => {
if (todo.id === action.id) {
return { ...todo, completed: !todo.completed }
}
return todo
})
default:
return state
}
}
const initialTodos = []
const store = createStore(todosReducer, initialTodos)
store.dispatch({
type: 'ADD_TODO',
id: 1,
text: ' redux '
})
store.getState()
store.dispatch({
type: 'TOGGLE_TODO',
id: 1
})
store.getState()
,
show must go on.
, .
combineReducers
, , reducer , .
:
function counterReducer(state, action) {
if (action.type === 'ADD') {
return state + 1
} else {
return state
}
}
const reducer = combineReducers({
todoState: todoReducer,
counterState: counterReducer
})
const initialState = {
todoState: [],
counterState: 0,
}
const store = createStore(reducer, initialState)
store , .
TodoApp .
ES6 (7/8/∞):
const reducer = combineReducers({ todos, counter })
todoReducer todos counterReducer counter. . , , redux, ,
(state.todos) ,
(function todos(){}).
micro-redux, :
function reducer(state, action) {
return {
todoState: todoReducer(state, action),
counterState: counterReducer(state, action),
}
}
. 2 «-»,
(state, action),
, ?, Object.entries
combineReducers (, )
:
function combineReducers(reducersMap) {
return function combinationReducer(state, action) {
const nextState = {}
Object.entries(reducersMap).forEach(([key, reducer]) => {
nextState[key] = reducer(state[key], action)
})
return nextState
}
}
redux 9 .
, , .
applyMiddleware
middleware redux — - , dispatch
-. , , ,… —
-.
middleware createStore, , :
const createStoreWithMiddleware = applyMiddleware(someMiddleware)(createStore)
const store = createStoreWithMiddleware(reducer, initialState)
applyMiddleware, 10 , : createStore «dispatch». dispatch, ( ) , — , (newState = reducer(state, action)).
applyMiddleware
dispatch, ( ) - .
, , middleware redux —
redux-thunk,
store.dispatch({type: 'SOME_ACTION_TYPE', some_useful_data: 1 })
store.dispatch
function someStrangeAction() {
return async function(dispatch, getState) {
if(getState().counterState % 2) {
dispatch({
type: 'ADD',
})
}
await new Promise(resolve => setTimeout(resolve, 1000))
dispatch({
type: 'TOGGLE_TODO',
id: 1
})
}
}
,
dispatch(someStrangeAction())
:
- store.getState().counterState 2, 1
- , todo id=1 completed true false .
, redux-thunk, redux — , ,
:
const thunk = store => dispatch => action => {
if (typeof action === 'function') {
return action(store.dispatch, store.getState)
}
return dispatch(action)
}
,
const thunk = store => dispatch => action
, , , , , (, , )
,
createStorefunction createStore(reducer, initialState) {
let state = initialState
return {
dispatch: action => { state = reducer(state, action) },
getState: () => state,
}
}
(reducer, initialState) { dispatch, getState }.
,
applyMiddleware , , .
createStore :
function applyMiddleware(middleware) {
return function createStoreWithMiddleware(createStore) {
return (reducer, state) => {
const store = createStore(reducer, state)
return {
dispatch: action => middleware(store)(store.dispatch)(action),
getState: store.getState,
}
}
}
}
redux . « , ». , 1 — .
P.S.
«micro-redux» store.subscribe 8 . ?