question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add new state to the Redux store?

See original GitHub issue

I am new to this project, and I am enjoying reading the code. But I am having a hard time figuring out how to add my app’s (new) state to the store.

The specifics of my app is that my server.js code will receive data asynchronously from some other server, which needs to be reflected in the GUI.

My understanding of the Redux pattern is that my server code should create actions for each newly-arrived bit of information and then dispatch them to the store. The update in the store then propagates to the React containers/components that rely on it.

I realize that this might be more of a Redux question, but I would love to see an outline of steps for the following in react-redux-universal-hot-example:

  1. Extending the ‘schema’ of the store with a new kind of information. (I’m not sure how to ensure that my new state data won’t conflict with existing info in the store);
  2. Creating actions (I think that’s straightforward - simply make a new object with { type: xxx, ... })
  3. Hooking those actions into the dispatch() method. (I see the @asyncConnect() and @ connect() methods, but don’t entirely understand what they do.)
  4. and whatever else needs to happen…

Thanks!

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:8

github_iconTop GitHub Comments

3reactions
tearsofphoenixcommented, Apr 12, 2016

Add new state is simple! Just write your own state file under redux/modules/, here is part of mine schedule.js:

// define constants for different actions
const UPDATE = 'uhs/schedule/UPDATE';
const UPDATE_SUCCESS = 'uhs/schedule/UPDATE_SUCCESS';
const UPDATE_FAIL = 'uhs/schedule/UPDATE_FAIL';

const SEARCH = 'uhs/schedule/SEARCH';
const SEARCH_SUCCESS = 'uhs/schedule/SEARCH_SUCCESS';
const SEARCH_FAIL = 'uhs/schedule/SEARCH_FAIL';

// initial state object, your may put more initial object here if needed
const initialState = {
  loading: false
};

// this will be used to `dispatch` actions, each `case` condition deal a kind of `action`
export default function reducer(state = initialState, action = {}) {
  switch (action.type) {
    case UPDATE:
    case SEARCH:
      return {
        ...state,
        loading: true
      };
    case UPDATE_SUCCESS:
      return {
        ...state,
        loading: false
      };
    case SEARCH_SUCCESS:
      return {
        ...state,
        loading: false,
        searchResult: action.result.data
      };
    case UPDATE_FAIL:
    case SEARCH_FAIL:
      return {
        ...state,
        loading: false,
        error: action.result.error
      };
    default:
      return state;
  }
}
// internal actions here
function _updateSchedule(data) {
  return {
    types: [UPDATE, UPDATE_SUCCESS, UPDATE_FAIL],
    promise: (client) => client.post('/schedule/update', {data})
  };
}

// this show how to `dispatch` sequential actions, See `clientMiddleware.js`
export function updateSchedule(data, callback) {
  return (dispatch) => {
    dispatch(_updateSchedule(data))
      .then(callback);
  };
}
// your action here, if it may have multiple `type` of result, use `types` field;
// note `post` (`data`) & `get` (`params`) has different arguments. See `clientMiddleware.js` under `redux/middleware` for detail
export function searchSchedule(params) {
  return {
    types: [SEARCH, SEARCH_SUCCESS, SEARCH_FAIL],
    promise: (client) => client.get('/schedule/search', {params}),
  };
}

@richb-hanover

0reactions
fabienheureuxcommented, Jan 5, 2017

@tearsofphoenix Your suggestion worked perfectly for me as well. That kind of answer really rocks 🤘

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux Fundamentals, Part 3: State, Actions, and Reducers
The official Redux Fundamentals tutorial: learn how reducers update state in response to actions.
Read more >
How To Manage State in React with Redux - DigitalOcean
Step 1 — Setting Up a Store · Step 2 — Creating Actions and Reducers · Step 3 — Dispatching Changes in a...
Read more >
Managing your React state with Redux - Medium
reducer - A reducing function. We will describe it below. · initialState - The initial state of the store. · enhancer - Can...
Read more >
How do I add an element to array in reducer of React native ...
Two different options to add item to an array without mutation case ADD_ITEM : return { ...state, arr: [...state.arr, action.newItem] }.
Read more >
Actions and reducers: updating state - Human Redux
Redux reducer rule #2: If you change it, replace it. ; // this is just creating another reference // to the same object...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found