Questions tagged [getderivedstatefromprops]

`getDerivedStateFromProps` is invoked right before calling the render method, both on the initial mount and on subsequent updates. It should return an object to update the state, or null to update nothing.

https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops

37 questions
37
votes
6 answers

Why getDerivedStateFromProps is called after setState?

React introduced new static method getDerivedStateFromProps(props, state) which is called before every render method, but why? Calling it after prop change makes sense to me but after setState it doesn't, maybe I am missing something. I was creating…
mukuljainx
  • 716
  • 1
  • 6
  • 16
5
votes
3 answers

ReactJS: Save input value on blur and not on every key stroke

I have created a React View, say MyView, which has 2 text inputs whose initial values will be passed by parent read from a DB. I also want the changed values to be saved back to DB. So, the view is also passed a callback function for the…
4
votes
2 answers

Does getDerivedStateFromProps actually update state?

I've read the documentation on reactjs.org regarding getDerivedStateFromProps. I've seen the use cases. And I understand why to use it. But I cannot figure out how it deals with the return value. Hence my question, when it does return... where does…
Acts7Seven
  • 417
  • 1
  • 6
  • 14
2
votes
2 answers

getDerivedStateFromProps returned undefined

Currently using getDerivedStateFromProps for the first time. My code works and it does what I want it to do, but I am getting a warning in my console that is confusing to me since my code is working. Warning : "getDerivedStateFromProps(): A valid…
eneooo
  • 368
  • 3
  • 11
2
votes
1 answer

Return one or two objects with getDerivedStateFromProps?

My component receives profile and errors from the parent. Within the component static getDerivedStateFromProps sets the new state should the component receive new data from the parent. ... constructor(props) { super(props); …
El Anonimo
  • 1,759
  • 3
  • 24
  • 38
2
votes
1 answer

Unable to use static getDerivedStateFromProps inside Redux + Typescript Connected Component

I am unable to use static method inside Class Component which is Connected to Redux. TypeScript reports Argument of type 'typeof SAI' is not assignable to parameter of type 'ComponentType'. Type 'typeof SAI' is not…
adyry
  • 613
  • 6
  • 15
1
vote
0 answers

Is this a valid use case for derived state from props? [react]

React documentation seems to be very insistent on the idea that in almost every situation, deriving state from props is a bad idea, an anti-pattern, verbose, likely to cause bugs, hard to understand, and all-around probably going to place a curse on…
1
vote
0 answers

How to Re-Render in React based on changes to a stately array variable

I'm trying to build a sorting visualizer similar to this: https://clementmihailescu.github.io/Sorting-Visualizer/ using React. To handle updating the Canvas with each step in the mergesort algorithm, I'm attempting to use a state variable yHeights…
1
vote
1 answer

How to compare two objects value by iterating if both have same name in getDerivedStateFromProps?

The props coming from react is account and I'm keeping a copy of it as previousProps, before setting it to the state I need to compare each and every object and assign the value to state if both props object are different. However, both previous…
1
vote
1 answer

getDerivedStateFromProps() vs componentDidUpdate()

I am using componentWillReceiveProps in many places in my application. Now, I have to replace them with either getDerivedStateFromProps() or componentDidUpdate(). First I thought of using getDerivedStateFromProps as it s alternative of…
ThinkAndCode
  • 1,319
  • 3
  • 29
  • 60
1
vote
1 answer

getDerivedStateFromProps when to clear data?

getDerivedStateFromProps is static for component. but today I debug the app, I find other components data nextProp.x in getDerivedStateFromProps method when we define it the same. the other situation is when a component is re-created, the…
Lenoarod
  • 3,441
  • 14
  • 25
1
vote
3 answers

React 16.4 - manual form input fill along with its updates from getDerivedStateFromProps?

I face a problem once update on React 16.4 where we have some breaking changes with getDerivedStateFromProps logic. Now it fires on each component update on both incoming and own component's props. So, I've read the docs and manuals, but still…
Max Travis
  • 1,228
  • 4
  • 18
  • 41
1
vote
1 answer

Is state update in getDerivedStateFromProps synchronous with the render?

Since getDervivedStateFromProps is called before every render, I assume the state is guaranteed to be updated synchronously before the render (i.e. it will not get batched with other setState calls like a normal setState call potentially would?) In…
1
vote
2 answers

Proper Way to merge new props into state

I am currently working on calendar component using react-big-calendar. I currently have one Wrapper for it that pulls data from an database and sends it into the Calendar Component. However, I am making a feature that one can 'send' calendar events…
1
vote
1 answer

How to call a method inside getDerivedStateFromProps in ReactJS?

I have a method inside my React class e.g. someMethod(). I also have a getDerivedStateFromProps() method in which I want to call someMethod(). Yes I need to do this in getDerivedStateFromProps and not in componentDidUpdate() for example because I do…
Valchy
  • 165
  • 3
  • 14
1
2 3