/common
/api
/components
/ducks
/entities
/sagas
/selectors
/utils
/features
/Feature1
/Feature2
/Feature3
/Feature4
...
/FeatureN
/Main
/pages
index.js
App.js
routes.js
rootReducer.js
rootSaga.js
store.js
/Auth
/pages
index.js
App.js
routes.js
rootReducer.js
rootSaga.js
store.js
...
/features
/Accounts
/components
index.js
accountsDucks.js
accountsSaga.js
accountsSelectors.js
accountsApi.js
Accounts.js
AccountsContainer.js
/actions
/common
/components
/core
/Feed
/Profile
...
/constraints
/containers
/entries
/locales
/pages
/reducers
/utils
...
export const withLatest = request => {
let last = 0;
return (...args) =>
new Promise((resolve, reject) => {
const current = ++last;
request(...args)
.then(res => resolve({ isLatest: current === last, res }))
.catch(err => reject({ isLatest: current === last, err }));
});
};
export const withLatest = request => {
let last = 0;
return async (...params) => {
const current = ++last;
try {
const res = await request(...params);
return { isLatest: current === last, res };
} catch (err) {
throw { isLatest: current === last, err };
}
};
};
import Api from '../api';
import { withLatest } from '../utils';
const withLatestFetchData = withLatest(Api.fetchData);
export const fetchData = query => async (dispatch, state) => {
dispatch({ type: "LOADING_DATA" });
try {
const { isLatest, res } = await withLatestFetchData(query);
if (isLatest) {
dispatch({ type: "DATA_SUCCESS", payload: res });
}
} catch ({ isLatest, err }) {
if (isLatest) {
console.log(err);
}
}
};
export const store = configureStore();
// some code
import { store } from './index';
import { someAction } from './actions/some';
store.dispatch(someAction());
const initialState = {
type_transport_id: null,
brand_id: null,
automodel_id: null,
year: null,
body_id: null,
};
export default function model(state = initialState, action) {
const { type, payload } = action;
switch(type) {
case SET_TYPE:
return {
...initialState,
type_transport_id: payload,
};
case SET_BRAND:
return {
...state,
brand_id: payload,
automodel_id: null,
year: null,
body_id: null,
};
default:
return state;
}
}
case SET_BRAND:
return {
...initialState,
type_transport_id: state.type_transport_id,
brand_id: payload,
};
function fetchSomeData(params) {
return async (dispatch, getState) {
try {
const { data } = await Api.fetchSomeData(params);
dispatch(fetchSomeDataSuccess(data));
// для доступа к store можно использовать getState
return data;
} catch (e) {
dispatch(fetchSomeDataFail(e));
return e;
}
}
}
const mapStateToProps = state => ({ ... });
const mapDispatchToProps = {
fetchSomeData,
};
export default connect(mapStateToProps, mapDispatchToProps)(SomeComponent);
И из-за этого не работает роутинг.
withRouter(connect(...)(MainLayout));
И зачем нужно писать экшен, если можно напрямую диспатч вызвать без посредников?
dispatch => ({
onSetScale: (event) => {
dispatch(setScale(event);
}
})
{
onSetScale: setScale,
}
componentDidMount() {
this.props.dispatch(action());
}
const computeDerivedState = props => ({
someData: props.someData,
});
class Example extends Component {
state = {
...computeDerivedState(this.props),
someState,
};
componentWillReceiveProps(nextProps) {
if (nextProps.someData !== this.props.someData) {
this.setState(computeDerivedState(nextProps));
}
}
}
const result = array.reduce((acc, el) => {
const key = 'Type' + el.type;
acc[key] = acc[key] ? [...acc[key], el] : [el];
return acc;
}, {});
И легально ли этим заниматься в редьюсере ? Может быть middleware лучше подойдет для этого?