JavaScript React-Redux component does not reflect changes in Redux Store react redux component not updating,react redux component not updating

I am new to Redux and i've been having a hard time rendering changes made to the store. I've been using Redux-DevTools to explore state changes and here is my problem.

I have a sidebar which has expanded state as true/false. Initial state is below.

{expanded: false}

During the toggle action i trigger store.dispatch to change the state to true. In React-DevTools i could see that my state is being changed, the console also logs the change when executed from within my sidenav component.

I have a home page which is able to fetch the initial state of the store, however the actions from sidenav which updates the store doesn't re-render(props val dint change) the home page.

I strongly feel the issue is related to this SO post but not able to get around the wrapped component concept. Any advice.

React Redux - changes aren't reflected in component

Below is code.

Edit p52155my9j

Redux Store

const rootReducer = combineReducers({
    sideBarReducerMain: sidebarReducer })

export const configurestore = () => {
    const store = createStore(rootReducer, composeWithDevTools(
      ));

    return store; }

Action generators

export const onExpand = {
    type: 'EXPAND',
    payload: {
        expanded: true
    }
}

export const onCollapse = {
    type: 'COLLAPSE',
    payload: {
        expanded: false
    }
}

Reducer

export const sidebarReducer = (state = {expanded:false},action) => {
 switch (action.type) {
     case 'EXPAND':
        return Object.assign({}, state, {expanded: true})
     case 'COLLAPSE':
        return Object.assign({}, state, {expanded: false})
    default:
        return state;   
 }
}

SideBar Toggle (The console logs the new state on every toggle)

onToggle = (expanded) => {

    expanded ? store.dispatch(onExpand):store.dispatch(onCollapse)
    this.setState({ expanded: expanded });

    //outputs the state change looks fine.
    console.log("State of store :: " + store.getState());

};

Home page

import React from 'react';
import styled from 'styled-components'
import Main from './MainAlign'
import { connect } from 'react-redux'

class HomePage extends React.Component {


 getExpansionState() {
  console.log("From render" + this.props.expandedState)
    return this.props.expandedState
 }

 componentWillReceiveProps(nextProps) {
  console.log("Looks like this never gets called :( " + this.props.expandedState)
}


  render(props) {

      return (
        <div>
        <Main expanded={this.getExpansionState()}>
        <h1>Welcome</h1>
        <p>This is my site. Take a look around!</p>
        </Main>  
        </div>
      )


  }

}

const mapStateToProps = state => {
  console.log("New state " + state.expanded)
  return {
    expandedState: state.expanded
  }
}

export default connect(mapStateToProps)(HomePage);

REDUX DevTools

ReduxDevTools

Answer:1

You need to declare action creators (functions) which return actions (objects):

export const onExpand = () => ({
  type: "EXPAND",
  payload: {
    expanded: true
  }
});

Then instead of calling store.dispatch, you should connect the action creator:

withRouter(connect(null, { onExpand, onCollapse })(SideBar))

And invoke it in your component:

if (expanded) {
  this.props.onExpand();
} else {
  this.props.onCollapse();
}

Working code:

Edit xj64o1j0kw

Misc

this.setState({ expanded: expanded });
console.log("FROM SIDENAV" + this.state.expanded);

setState is asynchronous so you need use the callback to access the updated value:

this.setState({ expanded }, () => console.log("FROM SIDENAV" + this.state.expanded));

Also, there's no need to replicate the redux state in local component (single source of truth).

You can also combine onExpand and onCollapse into a single onToggle function and perform the toggle in reducer: expanded: !state.expanded, or pass the expanded flag as payload.

Answer:2

I want to show pdf ajax response in browser.Problem is I can not directly open the url as pdf is created dynamically how can I show it. $.ajax({ url: url, type:...

I want to show pdf ajax response in browser.Problem is I can not directly open the url as pdf is created dynamically how can I show it. $.ajax({ url: url, type:...

By what logic js works (!+[]+[]+![]).length returns 9? How is that possible? As I know js has dynamic types cast but very hard to understand whats going on here console.log((!+[]+[]+![]).length);

By what logic js works (!+[]+[]+![]).length returns 9? How is that possible? As I know js has dynamic types cast but very hard to understand whats going on here console.log((!+[]+[]+![]).length);

  1. console log length of array
  2. console.log length
  3. console.log length limit
  4. console log length of string
  5. console.log max length
  6. intellij console log length
  7. javascript console.log length
  8. console.log('hi '.length)
  9. console.log maximum length
  10. eclipse console log length
  11. console.log array length 0
  12. console.log(data.length)
  13. console.log(array.length)
  14. javascript console.log max length
  15. javascript console log array length
  16. ie console log length
  17. console.log(foot.length)
  18. console.log($('ol li.even').length)

I'm trying to use the built in setValue() funtion in CRM 2015 to change a field back to null if a confirm() command comes back as false. The field that triggers the confirm() is also the field I am ...

I'm trying to use the built in setValue() funtion in CRM 2015 to change a field back to null if a confirm() command comes back as false. The field that triggers the confirm() is also the field I am ...

I want to make a command called _ram it should display the current usage of the bots ram. I already tried these things: ${client.performance.memory} //(Says memory is not defined) ${window....

I want to make a command called _ram it should display the current usage of the bots ram. I already tried these things: ${client.performance.memory} //(Says memory is not defined) ${window....

  1. check current memory usage linux
  2. check current cpu usage linux
  3. check current bandwidth usage
  4. check current ram usage linux
  5. check current psu usage
  6. check current data usage on iphone
  7. check current power usage pc
  8. check current ulimit usage
  9. check current internet usage
  10. check current network usage linux
  11. aws check current usage
  12. check current bandwidth usage linux
  13. check current sga usage
  14. check current pga usage
  15. check current tempdb usage
  16. check current memory usage python
  17. check current data usage verizon
  18. check current data usage at&t
  19. check current ram usage
  20. check current memory usage aix