• Stars
    star
    109
  • Rank 319,077 (Top 7 %)
  • Language
    JavaScript
  • Created about 6 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

⚛️ React-Redux application sample with all the best practices

React Redux Sample

Description

React application bootstrapped with Create React App for using with REST API and Redux for state managing.

Both components and redux-specific code (reducers, actions, action types) splitted by feature-first pattern Re-Ducks.

File structure

src/
├── state/        => represents redux
├── views/        => all react components
└── utilities/    => global constants and helper functions

Redux

State folder contains usual store.js and folder ducks, where one 'duck' equals one feature with one reducer. One duck contains actions.js, redurers.js, types.js and optional utils.js.

ducks/
├── duck/
|   ├── actions.js
|   ├── reducers.js
|   ├── types.js
|   ├── utils.js
|   └── index.js
└── index.js

Since index.js of each duck have default export as this feature's reducer, index of ducks folder represents root reducer. So adding a new, changing or deleting existing features in redux being not so painful - all files, related to one feature concentrated in one folder.

It also prevents merge conflicts in situations, when several people working around different features need to touch same files, as types, actions, etc.

// ducks/index.js
export { reducer as form } from "redux-form"

export { default as user } from "./user"
export { default as profile } from "./profile"

/* ... */

// store.js
import * as reducers from "./ducks"

export default createStore(
  combineReducers(reducers),
  reduxDevTools,
  applyMiddleware(...middlewares you use)
)

Index of ducks/ folder = old root reducer with x2 less more code

One more thing about reducers

There is a helper function, called createReducer, used to create reducers, not using basic switch-case template.

const someReducer = createReducer(initialState)({
  [types.YOUR_ACTION_TYPE]: (state, action) => {
    const some_var = "";
    return {
      ...state,
      some_prop: action.payload
    };
  },

  [types.SOME_ANOTHER_TYPE]: (state, { payload: { data } }) => ({
    ...state,
    data,
    loading: false
  }),

  [types.MAY_BE_YOU_WANT_RESET]: (state, action) => ({
    ...initialState
  })
});

Its very useful, for example, if you need to scope out part of reducer to use variables with same name in several case statements.

Tip: switch-case template still can be useful when several types causes same reaction.

About actions

To handle asynchronous actions we usually using redux-thunk middleware and always using action creators.

const someAction = payload => ({
  type: types.SOME_YOUR_TYPE,
  payload
});

const someFetchAction = payload => (dispatch, getState) => {
  dispatch(setLoading(payload.id));

  fetch(GET, `/api_endpoint?some_parameter=${payload.id}`)
    .then(response => {
      if (getState().yourReducer.currentLoading === payload.id) {
        dispatch(setLoaded(response));
      }
    })
    .catch(error => {
      dispatch(setFail(error));
      console.error(error);
    });
};

React

views/
├── routes/       => base router
├── components/   => feature-first components
├── pages/        => layouts, related to routes
├── styled/       => StyledComponents
└── UI/           => reusable components

We splitting components to two parts - Container and Component.

Container file concentrates in itself all logic and HOCs of this feature.

Component itself usually a plain stateless component.

// FeatureContainer.js

import Feature from './Feature.jsx'

const withConnect = connect(...)

const withForm = reduxForm({
  ...
})

const enhance = compose(
  withConnect,
  withForm,
  anyOtherListedHOC
)

export default enhance(Feature)

// Feature.jsx

const Feature = ({props you needed}) => (
  /* some jsx code here */
)

export default Feature

License

react-app-best-practice is Copyright © 2015-2019 Codica. It is released under the MIT License.

About Codica

Codica logo

We love open source software! See our other projects or hire us to design, develop, and grow your product.

More Repositories

1

rspec-best-practices

Examples of RSpec code
Ruby
63
star
2

angular-best-practices

Best Practices for Writing Angular Apps
59
star
3

mina-multideploy

🚀 Parallel deploying on multiple servers with mina.
Ruby
52
star
4

administrate-field-jsonb

A plugin to show and edit JSON objects within Administrate.
JavaScript
44
star
5

rails-app-best-practice

Rails app structure example
Ruby
42
star
6

rails-puma-ssl

🔐 Easy way to start using SSL in development
40
star
7

simple-sidenav

Simple, easily customizable, animated menu.
TypeScript
39
star
8

vue-app-best-practice

Vue application sample with all the best practices
JavaScript
38
star
9

nextjs-graphql-sample

A simple app to demonstrate basic API functions built with REST and GraphQL
TypeScript
31
star
10

pull-request-best-practices

A simple list of recommendations of the pull request best practices
Ruby
31
star
11

timebot

🤖 Timebot is a Slack bot for helping with everyday timesheet
Ruby
28
star
12

rails-api-documentation

Example of how to generate the API documentation in your rails app
Ruby
23
star
13

rails-api-template

Application template for Rails 5 API
Ruby
23
star
14

cypress-best-practices

How we test frontend applications
21
star
15

ubuntu-laptop-script

Set up an Ubuntu laptop for web development
Shell
18
star
16

rubicon

Easy project version management.
Ruby
18
star
17

rest-graphql

A simple app to demonstrate basic API functions built with REST and GraphQL
Ruby
17
star
18

hv-autocomplete

Fast and flexible autocomplete library
JavaScript
17
star
19

clean-code-tools

How we configure linters for Ruby on Rails projects
16
star
20

script-best-practices

Best practices for writing bash scripts
Shell
15
star
21

rails-multi-environment-seeding

Example of using seeds in rails app
13
star
22

timebot-go

🤖 Timebot is a Slack bot for helping with everyday timesheet
Go
13
star
23

gitlab-ci-configuration

Example of configuration Gitlab CI for Rails Application
12
star
24

vue-timebot

🤖 Timebot is a Slack bot for helping with everyday timesheet
Vue
12
star
25

google-map-popover-example

⚛️ An example of React custom popover for Google maps
JavaScript
11
star
26

simple-scraper

A fairly simple gem that will help you simplify the parsing of web pages.
Ruby
11
star
27

production-server-security-notes

🔓 7 tips to protect your production server
8
star
28

business-analytics-tools

How we use business analytic tools
7
star
29

ansible-best-practice

Simple IT automation engine
7
star
30

auth-best-practices

Token-based Authentication Best Practices
7
star
31

scrapinghub-ruby-example

Simple guide to working with Scrapinghub
Ruby
7
star
32

react-site-tutorial

Site tutorial for ReactJS
JavaScript
6
star
33

eslint-config-codica

This package provides Codica's .eslintrc as an extensible shared config.
JavaScript
6
star
34

ssh-management-with-ansible

Easy-in-use and cost-efficient solution for SSH management
5
star
35

site-tutorial

Is easy, animated, a lot of functionality, flexible, step tutorial for your site.
CSS
4
star
36

gitlab-bastion-ci-aws

Cost-effective and scalable solution for you CI/CD
4
star
37

services-monitoring-with-monit

Managing and monitoring Unix system processes with Monit
3
star
38

vue-simple-tree-table

Vue tree table is a vue js table component, with a tree functionality.
Vue
3
star
39

react-authorized-routes-sample

⚛️ React authorized routes with HOC sample
3
star
40

sample-article-code-elasticsearch-rails

Sample code from an article
Ruby
2
star
41

gitlab-aws-ecr

Autoscaling GitLab Runner on AWS with ECR authentication
2
star
42

reactivesearch-aws-es-proxy

Simple lambda created for connection your Reactivesearch with AWS Elasticsearch.
JavaScript
2
star
43

mongo-dump

simple docker image for mongo-dump and copy to s3
Shell
2
star
44

redirect-s3-cf

This guide will show you how to redirect non-www requests to www with S3 and CloudFront.
2
star
45

linux-setup

Bash script for setting Linux!
Shell
1
star
46

terraform-with-pagerduty

How to create service in Pagerduty via Terraform
HCL
1
star