Skip to content

Tag: javascript

Where to write to localStorage in a Redux app?

I want to persist some parts of my state tree to the localStorage. What is the appropriate place to do so? Reducer or action? Answer Reducer is never an appropriate place to do this because reducers should be pure and have no side effects. I would recommend just doing it in a subscriber: Before creating the s…

Getting the angle from a direction vector?

I have this simple function to set an angle for a vector. It effectively gets the vector’s current magnitude (length), calulates the angle and converts the angle from radians to degrees. Then I apply the angle to X and Y, lastly multiplying the vector by it’s original magnitude. However I am unsur…

Add array of elements to dom

I have 5 div’s created in HTML, and I want to add all of them into a div wrapper I created in JavaScript. I tried looping through the 5 div’s via a for-in loop, then append the div as a child of the wrapper. For some reason, the for loop changes the 5 div’s order and doesn’t append all…

Calling setState in a loop only updates state 1 time

Is there a reason that calling setSate() in a loop would prevent it from updating the state multiple times? I have a very basic jsbin that highlights the problem I am seeing. There are two buttons. One updates the state’s counter by 1. The other calls the underlying function of One in a loop — whi…