Giter Site home page Giter Site logo

react-test's Introduction

Trying out the React stack

My personal playground of react, redux, sagas, selectors and more fun stuff!

Notes

Dan Abramov's video series Getting Started with Redux, is a great starting point on your Redux journey.

Interesting article series from Jack Hsu, regarding structure of Redux application. Three Rules For Structuring (Redux) Applications

Bumps in the road

Soon enough I ended up with a nested store, and my reducer looked like shit.

const counterList = (state = [], action) => {
  switch (action.type) {
    case 'CREATE':
      return [
        ...state,
        {
          id: state.length,
          value: 0,
        }
      ]
    case 'DESTROY':
      return [...state.slice(0, state.length - 1)]
    case 'INCREMENT':
      return [ ...state.slice(0, action.id),
        {
          id: action.id,
          value: state[action.id].value + 1,
        },
        ...state.slice(action.id+1)
      ]
    .
    .
    .
    default:
      return state
  }
}

By normalizing state as described in Normalizing State Shape of the Redux docs, you end up with simpler reducers.

import { combineReducers } from 'redux'
import _ from 'lodash'

const allIds = (state = [], action) => {
  switch (action.type) {
    case 'CREATE':
      return state.concat(state.length+1)
    case 'DESTROY':
      return state.slice(0, state.length-1)
    default:
      return state
  }
}

const objectLength = o => {
  if (o) return Object.keys(o).length
  else   return 0
}

const byId = (state = {}, action) => {
  switch (action.type) {
    case 'CREATE':
      return {
        ...state,
        [objectLength(state)+1]: {
          value: 0
        }
      }
    case 'DESTROY':
      return _.omit(state, objectLength(state))
    case 'INCREMENT':
      return {
        ...state,
        [action.id]: {
          value: state[action.id].value + 1
        }
      }
    .
    .
    .
    default:
      return state
  }
}

const counter = combineReducers({
  listAllIds: allIds,
  listById: byId,
})

Ending up with a store which looks like:

{
  "counters": {
    "listAllIds": [ 1, 2 ],
    "listById": {
      "1": { "value": 42 },
      "2": { "value": -1 }
    }
  }
}

When working with deeply nested data structures, the normalizr library is good for parsing complicated data (i.e. data received from an API call to the back-end).

Yes, we need to be just as mindful with normalizations and the structure of data, as when designing relational databases on the back-end.

Pls let me view state changes

Logging to the rescue!

Redux Logger

We use the middleware package redux-logger.

import { createStore, applyMiddleware } from 'redux'
import createLogger from 'redux-logger'
import rootReducer from './rootReducer'

const logger = createLogger()
const store = createStore(
  rootReducer,
  window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(),
  applyMiddleware(logger)
)

render(
  <Provider store={store}>
    ...
  </Provider>,
  document.getElementById('root')
)

react-test's People

Contributors

pinne avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.