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.

Props undefined || state update not updating props

See original GitHub issue

Hi. I’m pretty new to redux and I’ve been trying to learn and build my project with redux but I’m totally baffled as this boilerplate doesn’t just use Redux but also some other stuff such as Immer and Reselect. (No offense though xD) I have a container where I fetch some data, then that data is put in the store. But the problem is, when I log props, they show me the initial state and therefore there is no re-rendering, yet Redux DevTools is showing me that the states have been updated. Also, I would appreciate if somebody could tell me why I get undefined on get/getIn when I use state.get or state.getIn to retrieve the state in selector and my container. Because I saw some examples in using this code, so maybe this has something to do with my problem!

Here is some portions of my code:

// Blog container (Index.js)
const mapStateToProps = createStructuredSelector({
  title: makeSelectTitle,
  posts: makeSelectBlog,
});

export function mapDispatchToProps(dispatch) {
  return {
    fetchPosts: () => dispatch(getBlogPosts()),
  };
}

const withConnect = connect(
  mapStateToProps,
  mapDispatchToProps,
);

const withReducer = injectReducer({ key: 'blog', reducer });
const withSaga = injectSaga({ key: 'blog', saga });

export default compose(
  withConnect,
  withReducer,
  withSaga,
)(withStyles(styles)(Blog));
// reducer of blog container
export const initialState = {
  title: 'first title',
  posts: {},
};

/* eslint-disable default-case, no-param-reassign */
const blogReducer = (state = initialState, action) =>
  produce(state, draft => {
    // const posts = [...state.posts];
    switch (action.type) {
      case GET_BLOG_POSTS:
        return state;
      case LOADED_BLOG_POSTS:
        return {
          ...state,
          posts: action.posts,
          title: 'new title',
        };
      // break;
      default:
        return state;
    }
  });
// Selector of blog container

const selectBlogPosts = state => state.posts || initialState;
const selectBlogTitle = state => state.title || initialState;

const makeSelectBlog = createSelector(
  selectBlogPosts,
  substate => substate,
  blogState => blogState.posts,
);

const makeSelectTitle = createSelector(
  selectBlogTitle,
  substate => substate,
  blogState => blogState.title,
);

export { selectBlogPosts, selectBlogTitle, makeSelectBlog, makeSelectTitle };

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
julienbencommented, Aug 19, 2019

It depends on the size of your app. Reselect is only about helping you with performance (avoiding unnecessary re-renders) but it’s often misused anyway so unless you’re building a huge app with major performance concerns, it’s ok to skip it.

0reactions
lock[bot]commented, Sep 18, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

props is undefined although state has changed - Stack Overflow
In file Navbar.jsx , value of this.state.Tot_Item is empty array. Use this.setState function to change the value of this.state.
Read more >
React.Component
Updating. An update can be caused by changes to props or state. These methods are called in the ... You should not call...
Read more >
Connect: Extracting Data with mapStateToProps - React Redux
The first argument to a mapStateToProps function is the entire Redux store state (the same value returned by a call to store.getState() )....
Read more >
How to use Props in React - Robin Wieruch
In the last example, the App component uses a stateful value called isShow and a state updater function to update this state in...
Read more >
React Props Cheatsheet: 10 Patterns You Should Know
6. Update a React prop's value with state. Props cannot be directly updated. To pass a prop value to a component, we cannot...
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