A
A
AlexKindGeek2018-07-13 22:34:04
React
AlexKindGeek, 2018-07-13 22:34:04

Questions about React'y / Redux'y?

Hello. In order not to create questions 10 times, I will write in one, everything that interests me.
1. In real projects, do you use CRA or do you set up the project yourself (along with webpack, etc.)?
2.

How do you make reducers?
Например у нас есть 5-10+ екшенов, которые меняют один и тот же редюсер. Это же получится каша, если использовать switch ....
Используете ли вы функцию по типу
createReducer(initialState, mainAction, {
  actionName:  reducerHelper
})
???
Пример
const createQuestionUpdate = (state, action) => {
  switch (action.type) {
    case `${CREATE_QUESTION}_UPDATE`:
      const {data, path } = action.payload;
      const res =  set(state, path, data);
      return Object.assign({}, res);
    default:
      return state;
  }
};

export default createReducer(Object.assign({}, InitialState), CREATE_QUESTION, {
  [`${CREATE_QUESTION}_UPDATE`]               : createQuestionUpdate
});


3.
How do you handle forms?

Например у меня на проекте в одной компоненте есть +-5-7 инпутов, и на каждый из них повешан onChange по типу
Пример
export const onChange = (e, val = "value") => dispatch => {
  let type = e.target.name; //actionType
  let property = e.target.id;
  let value = e.target[val]; // input value
  let { errors } = validate[type]({ [property]: value }); // error object for all related inputs

  dispatch([
    {
      type: type,
      payload: { [property]: value }
    },
    {
      type: `${type}_ERROR`,
      payload: {
        errors: { [property]: get(errors, property) }
      }
    }
  ]);
};

который изменяет нужную филду в нужном стейте. Нормально ли это?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-07-13
@AlexKindGeek

1.
By ourselves 2. We write with pens (+10 to readability, +10 to static analysis, -100 to cognitive load) Of course, if you write a dashboard with a bunch of models, it makes sense to write a CRUD Boilerplate
3. In 99.99% of cases, storing the state of forms in the store is meaningless and unjustified

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question