JavaScript In React, what is the best way to define action types? react best practices,react best practices 2019,react best practices

In React, which one of the following ways is the best way to define action types?

First way:

Defining actions using strings like the following:

const actionCreatorExample = (value) => {
  return { type: 'SET_RESPONSE', value };
}

Second way:

Defining action types in an object and referring to action types by accessing the values of that object. Like this:

export const actionTypes = {
  SET_RESPONSE: 'SET_RESPONSE'
};

import actionTypes from './actionTypes';

const actionCreatorExample = (value) => {
  return { type: actionTypes.SET_RESPONSE, value };
}

The second way looks neat but why waste memory by storing the actionTypes object? Any thoughts?

Answer:1

The way I use.

Folder structure

-- actions -- operations -- types -- reducers

// types
const IS_MOBILE = 'root/IS_MOBILE';
const SCROLL_POS = 'root/SCROLL_POS';
const OPEN_MENU = 'root/OPEN_MENU';

export {
  IS_MOBILE,
  SCROLL_POS,
  OPEN_MENU,
};

// actions

export const getResolution = boolean => ({
  type: actionType.IS_MOBILE,
  payload: boolean,
});

export const getScrollPos = () => ({
 type: actionType.SCROLL_POS,
 payload: window.pageYOffset || document.documentElement.scrollTop,
});


export const stateMenu = boolean => ({
  type: actionType.OPEN_MENU,
  payload: boolean,
});
Answer:2

I wouldn't say "best way", but here's how I do it. I have a helper function to build my actions like this:

const action = (type, payload = {}) => ({ type, payload });

export default function buildSimpleAction(baseName) {
  return {
    actionType: baseName,
    actionCreator: args => action(baseName, { ...args })
  };
}

I declare them as:

import buildSimpleAction from 'redux/buildSimpleAction';

export const { actionType: DISTINCT_ID, actionCreator: setDistinctId } = buildSimpleAction('DISTINCT_ID');

And finally I can use them like:

import { DISTINCT_ID, setDistinctId } from 'redux/actions'

It may seem complicated to use a helper function to create a simple action, but I have other types of actions that are not 'simple', like API calls. The api helper returns, instead of a single action, an object with several actions inside (request, success, failure...).

Answer:3

Both are preferable. First case can be a little cleaner in small apps. As redux docs says:

You don't have to define action type constants in a separate file, or even to define them at all. For a small project, it might be easier to just use string literals for action types. However, there are some benefits to explicitly declaring constants in larger codebases. Read Reducing Boilerplate for more practical tips on keeping your codebase clean.

https://redux.js.org/basics/actions

Second case is more preferable in large apps. Because there can be a lot of types and you can make a typo. So better if you define them in one place and then simply use from that place.

Answer:4

That's rather an opinion-based question, however it tend to be a better idea to store all the const, types and interfaces related with specified part of your project in a separated file, called model.js or constants.js. The main profit out of it - besides it helps to keep your code cleaner - would be the reusability.

If you would ever decide to use TypeScript and union types or discriminated unions, it will be much easier for you to just use a variable which holds the action type than writing it once again by hand (also you will reduce a chance to misspell the action type name).

Summarizing - I would vote for the second method. And that's how I'm doing it in my every project.

Answer:5

Consider a custom directive in angularjs: .directive('myDir', function() { return { restrict: 'E', template: "..." } }) As far as I see the tag <my-dir></myDir> has no ...

Consider a custom directive in angularjs: .directive('myDir', function() { return { restrict: 'E', template: "..." } }) As far as I see the tag <my-dir></myDir> has no ...

  1. deal with custom
  2. deals custom woodworks
  3. how to deal with custom officer
  4. custom deal with it gif

let suppose I have an array var a = [1,2,3,4] and now I want to print this array from the back side like 4,3,2,1 I know this can we achieve by this for(let i=a.length; i>= 0; i--) { console....

let suppose I have an array var a = [1,2,3,4] and now I want to print this array from the back side like 4,3,2,1 I know this can we achieve by this for(let i=a.length; i>= 0; i--) { console....

  1. print array element python
  2. print array element bash
  3. print array element php
  4. print array elements in java
  5. print array elements in c
  6. print array elements javascript
  7. print array elements in reverse order java
  8. print array elements in shell script
  9. print array elements perl
  10. print array elements using recursion java
  11. print array elements in reverse order c++
  12. print array elements in new line javascript
  13. print array elements in reverse order javascript
  14. print array element matlab
  15. print array elements in ascending order
  16. print array elements using recursion in c
  17. print array elements in one line java
  18. print array elements using pointers
  19. print array elements in scala
  20. print array elements in java 8

I've written the following code that generates a select list. However, the part where it's supposed to check if this.id equals the same as the existing_code and put the selected value in doesnt seem ...

I've written the following code that generates a select list. However, the part where it's supposed to check if this.id equals the same as the existing_code and put the selected value in doesnt seem ...

  1. javascript foreach select options
  2. javascript foreach select

In VueJS, I have seen different ways of accessing parent properties from a component. Say I want to use the parent property items in my component. First way The component has a props value bound to ...

In VueJS, I have seen different ways of accessing parent properties from a component. Say I want to use the parent property items in my component. First way The component has a props value bound to ...