V
V
vaskadogana2017-03-09 17:46:15
React
vaskadogana, 2017-03-09 17:46:15

Why is props not updated after unshift and dispatch?

there is a code like this

.then(function answer(answer){
        	let clients_info = answer.data;
        	<b>currentList.unshift(clients_info);</b>
        	store.dispatch({
        		type: 'GET_clients',
        		setAdminData: currentList
      })
    })

store is updated but no rendering occurs. (new props do not arrive)
If I set a variable without unshift, then rendering occurs.
like this for example
.then(function answer(answer){
        	let <b>clients_info</b> = answer.data;
        	currentList.unshift(clients_info);

        	store.dispatch({
        		type: 'GET_clients',
        		setAdminData: clients_info
      })
    })

From this I conclude that the problem is not in connections, etc. And in the array that is returned from this line
currentList.unshift(clients_info);
visually, the array became one element larger. Took the current state, added an element to the beginning of the array. made dispatch of a new array. However, rendering does not occur.
Has anyone come across?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2017-03-09
@vaskadogana

Took the current state, added an element to the beginning of the array. made dispatch of a new array.

Here lies the main mistake, you "made a dispatch" of the same array. Why did he become new? Not true.
Those. you return the same object (in js objects are passed by reference), nothing has changed for the react view, so you don’t need to call render => nothing happens.
Try to return a new(!) array, in your case it is:
Or a little clearer, but inserting at the end of the array:
The concat method always returns a new array.
Well, to make it completely clear what's going on, try executing the following code in the console:
let arr1 = [1,2,3,4]
let arr2 = arr1
arr2.unshift(1)
console.log(arr1)

Questions: what is arr1 equal to? Why? Will arr1 === arr2 why?
More questions: When does react call the render function?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question