Andrey Sinitsyn
Общее
React
Redux
<MessageComponent
message={this.state.currentMessage}
shouldComponentGetHighlighted={this.state.isInFocus}
{...filteredMessageProps}
onClick={this.boundMessageClickHandler}
messageType={this.getMessageType(this.state.currentMessage)}
messageMeta={this.getMessageMeta(this.state.currentMessage)}
messageActions={this.props.messageActions}
isDeleted={confirmDeletion(this.state.currentMessage)}
userSelector={this.props.userSelector}
dialogDataSelector={this.props.dialogDataSelector}
contactsMetaSelector={this.props.contactsMetaSelector}
routingSelector={this.props.routingSelector}
/>
npm i -S reprovide
...Dependency Injection.
В Реакте.
Прощай, миллард пропов, передаваемых компоненту!
Два типа компонентов:
import {
Provider,
ProviderComponent
} from 'reprovide';
@Provider('RandomValueGeneratorProvider')
class RandomValueGeneratorProvider
extends ProviderComponent {
shouldComponentReprovide(nextProps, nextState) {
return true;
}
getProvider() {
return {
value: () => Math.random()
}
}
}
import React from 'react';
import { Inject } from 'reprovide';
import RandomNumberGeneratorProvider
from '../RandomNumberGeneratorProvider';
@Inject(
RandomNumberGeneratorProvider
)
class RandomNumberDisplay extends React.Component {
render() {
const {
providers: {
RandomNumberGeneratorProvider
}
} = this.props;
return (
<div>RNG got us: {RandomNumberGeneratorProvider.value()}</div>
);
}
}
Inject - HOC, позволяющий оборачивать компонент в Провайдеры, которые мы ему передали.
Записи далее эквивалентны
@Inject(SomeProvider)
class RandomComponent extends React.Component {
render() {
const {
providers: { SomeProvider }
} = this.props;
return SomeProvider.someValue;
}
}
// later
<RandomComponent />
class RandomComponent extends React.Component {
render() {
const {
providers: { SomeProvider }
} = this.props;
return SomeProvider.someValue;
}
}
// later
<SomeProvider>
<RandomComponent />
</SomeProvider>
npm i -S reprovide
switch(action.type) {
case 'UPDATE_MESSAGES_SUCCESS':
return updateStateUpdate(state, action.payload);
case 'FETCH_MESSAGES_SUCCESS':
return updateStateFetch(state, action.payload);
default:
return state;
}
npm i -S reducio
Принимает
Возвращает
Принимает:
Возвращает
const x = composeReducers(reducer1, reducer2, reducer3)
// внутреннее устройство x
function x(state, action) {
return reducer1(
reducer2(
reducer3(state, action),
action
),
action
);
}
const createActionEqualityPredicate =
value =>
action =>
action.type === value
const updateStateKey =
key =>
(state, action) =>
immutablyUpdate(state,
{ [key]: { $set: action.payload } })
// immutablyUpdate - ваша утилита
// для иммутабельного обновления данных
// Ещё одна утилита, условно доступная глобально
const genActionType =
(duck, entity, action, status) =>
`app/${duck}/${entity}/${action}/${status}`
const commonEntityUpdater =
(state, action) =>
updateStateKey(`list.${action.payload._id}`)
(state, action)
const createCreationReducer = (duck, entity) => (
createReducer(
createActionTypeEqualityPredicate(
genActionType(duck, entity, 'create', 'success')
),
commonEntityUpdater
)
)
const createRetrievalReducer = (duck, entity) => (
createReducer(
createActionTypeEqualityPredicate(
genActionType(duck, entity, 'retrieve', 'success')
),
updateStateKey('list')
)
)
const createUpdateReducer = (duck, entity) => (
createReducer(
createActionTypeEqualityPredicate(
genActionType(duck, entity, 'update', 'success')
),
commonEntityUpdater
)
)
const createDeleteReducer = (duck, entity) => (
createReducer(
createActionTypeEqualityPredicate(
genActionType(duck, entity, 'delete', 'success')
),
(state, action) =>
updateImmutably(
state,
{
[`list.${action.payload._id}`]:
{ $delete: true }
}
)
)
)
const createEntityReducer = (duck, entity) => (
composeReducers(
createCreationReducer(duck, entity),
createRetrievalReducer(duck, entity),
createUpdatingReducer(duck, entity),
createDeletingReducer(duck, entity)
)
)
const messagesReducer = createEntityReducer(
'messages',
'message'
)
1. npm i -S reducio
2. ???
3. PROFIT!