34

Below is my component class. The component never seems to execute componentWillUpdate(), even when I can see the state updating by logging before the return in mapStateToProps. The state is 100% changing, however the component doesn't refresh.

import React, { Component } from 'react'
import { connect } from 'react-redux'
import { search } from './mapActions'
import L from 'leaflet'


class Map extends Component {
  componentDidMount() {
    L.Icon.Default.imagePath = './images'
    this.map = new L.Map('map', {
      center: new L.LatLng(this.props.lat, this.props.lng),
      zoom: this.props.zoom,
      layers: L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
        attribution: '<a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
      })
    })
  }
  componentWillUpdate() {
    console.log('UPDATE MAP')
    L.geoJson(this.props.data).addTo(this.map)
  }
  render() {
    return <div id="map"></div>
  }
}

const mapStateToProps = (state) => {
  return {
    isFetching: state.isFetching,
    data: state.data
  }
}

const mapDispatchToProps = (dispatch) => {
  return {
    search: (name) => {
      dispatch(search(name))
    }
  }
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(Map)

And here is the map reducer:

const initialState = {
  isFetching: false,
  data: {}
}

export const map = (state = initialState, action) => {
  switch(action.type) {
    case 'REQUEST_SEARCH_RESULTS':
      return Object.assign({}, state, {
        isFetching: true
      })
    case 'RECEIVE_SEARCH_RESULTS':
      return Object.assign({}, state, {
        isFetching: false,
        data: action.data
      })
    default:
      return state
  }
}

After some more testing and logging it seems that when it goes to map state to props the state object it uses to map to props contains the correct data, so state.map.data is correct and I can see the return from the fetch. However when I then log this.props in componentWillUpdate(), the data object is there but empty.

Jacob Mason
  • 1,355
  • 5
  • 14
  • 28

6 Answers6

44

I had a similar problem and I found out the answer after read this:

Data gets set/updated/deleted in the store via the results of handling actions in reducers. Reducers receive the current state of a slice of your app, and expect to get new state back. One of the most common reasons that your components might not be re-rendering is that you're modifying the existing state in your reducer instead of returning a new copy of state with the necessary changes (check out the Troubleshooting section). When you mutate the existing state directly, Redux doesn't detect a difference in state and won't notify your components that the store has changed. So I'd definitely check out your reducers and make sure you're not mutating existing state. Hope that helps! (https://github.com/reactjs/redux/issues/585)

When I tried use Object.assign({}, object) as you, it didn't work anyway. So was when I found this:

Object.assign only makes shallow copies. (https://scotch.io/bar-talk/copying-objects-in-javascript)

Then I understood that I had to do this: JSON.parse(JSON.stringify(object))

or just this: {...object}

For Arrays: [...theArray]

I hope that this will help you

Aakash
  • 21,375
  • 7
  • 100
  • 81
Marina
  • 1,682
  • 1
  • 20
  • 25
31

Because you're not changing the reference, so React's shallow compare doesn't detect the update.

I'm going to use a simple example with blog posts. In your reducer, you're probably doing something as follows:

case FETCH_NEW_POSTS
    let posts = state.posts;
    posts.push(action.payload.posts);
    return {
        ...state, 
        posts
    };

Instead of that, you must do something like the following:

case FETCH_NEW_POSTS
    let posts = [...state.posts]; // we're destructuring `state.posts` inside of array, essentially assigning the elements to a new array.
    posts.push(action.payload.posts);
    return {
        ...state, 
        posts
    };

Depending on your use case Object.assign() or lodash's clone/deepclone may be more idiomatic.

Nathanael
  • 954
  • 3
  • 19
  • 39
  • THIS. Thanks, this is exactly the issue, should be the top answer – Sean Mar 22 '19 at 21:36
  • 1
    Just to reword it, in the first case you are pushing to an array, which modifies it, but the array keeps the same reference in memory. So when you return posts, you're returning still the same array, and so the state doesn't update. In the 2nd case you are creating a copy of the array. – Scruffy Oct 28 '19 at 11:37
  • This solution should be on top, its the perfect solution – Akhil Aravind Apr 19 '20 at 17:11
  • This was exactly the issue I was trying to resolve for a day. Helped me a lot ;) – Abhishek Bhagate Jul 10 '20 at 14:00
  • I don't see why that changes it... isn't the first code block still returning a new object with a new address in memory? Would love for someone to clarify this for me. – dillon.harless Nov 11 '20 at 21:35
  • 1
    @dillon.harless, no, in JS objects are a reference type. In the first block you're setting ```posts``` to the same reference as `state.posts`. Since React's shallow compare notices the same object pointer, it doesn't get updated. See more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects particularly the "Comparing objects" section at the bottom. – Nathanael Nov 13 '20 at 01:59
  • This answer is still valid Dec 07 2020. spent hours debugging this. – David Dec 07 '20 at 22:24
8

componentWillUpdate receives the incoming props as an argument. At this time, this.props is still the old props. Try changing your method like so:

void componentWillUpdate(nextProps, nextState) {
    L.geoJson(nextProps.data).addTo(this.map);
}
Brandon
  • 38,310
  • 8
  • 82
  • 87
  • Hi Brandon, thank you for the response. The function still doesn't seem to get called. In my three actions, request, receive and fetch, I have logged to console and it all seems to go through fine. I also did some logging in the reducer, and the state definitely changes. – Jacob Mason Mar 15 '16 at 14:23
  • you'll need to show the code for your actions and the code that is dispatching those actions. – Brandon Mar 15 '16 at 14:50
  • Hi Brandon, just found something odd that might assist. Check the question for the extra detail. – Jacob Mason Mar 16 '16 at 11:35
  • Ah, I think I solved it... it seemed the data was there during componentDidUpdate() but not componentWillUpdate(). Isn't that pointless to have the data post-update? Surely you need the new data pre-update? – Jacob Mason Mar 16 '16 at 11:39
0

Make sure you are listening to that store in your maps file if you are passing props to your component through maps file.

export const listenToStores = [CommonStore, store];
@connection(maps.listenToStores, maps.getStateFromStores)
  • It seems like this might be the answer for my scenario, but where is the `connection` function imported from? – MikeyE Apr 25 '21 at 21:55
0

Building on what Marina answered going from

var tempArray = imageArrayState
tempArray.push(5)
setImageArray(tempArray)

to

var tempArray = []
imageArrayState.foreach(value => {tempArray.push(value)})
tempArray.push(5)
setImageArray(tempArray)

made my app refresh

Joe Giusti
  • 159
  • 1
  • 3
0

In my case I had same problem mentioned in the question and I have resolved it using new array create using spread operater instead of using array push method.

Error: rows.push(row)

const onAddRow = useCallback(rowIndex => {
        const rows = props.managefunds
        const row = rows.find((row, index) => {
            return rowIndex === index
        })
        rows.push(row) // while directly push in to array, page is not refreshing
        props.handleSubAccount({label: 'subAccount', value: rows })
    }, [ props.managefunds ])

Solution: [ ...rows, row ]

const onAddRow = useCallback(rowIndex => {
        const rows = props.managefunds
        const row = rows.find((row, index) => {
            return rowIndex === index
        })
        props.handleSubAccount({label: 'subAccount', value: [ ...rows, row ]}) // while create new array in the following way, the page is refreshing properly
    }, [ props.managefunds ])
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133