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
1
vote
1 answer

React Hooks FAQ: implementation of getderivedstatefromprops leads to rendering with incoherent state

IMHO The suggestion from React Hooks FAQ #getDerivedStateFromProps leads to one first rendering with a value of row that doesn't correspond to the value of isScrollingDown . As the call to setIsScrollingDown only schedules a new rendering and…
bbo
  • 13
  • 4
1
vote
0 answers

List component functionality

Just wondering if anyone could take a look at my code on sandbox to help me with a problem I have been struggling with. SelectList Display a full list of options to add to the List Display Pre-selected options (items with a value > 0) Updated when…
1
vote
1 answer

Refactoring UNSAFE_componentWillReceiveProps

I have an IFrameComponent component, inspired by this post. It looks basically like this : class IFrameComponent extends React.Component { shouldComponentUpdate() { return false; } componentWillReceiveProps(nextProps) { …
Logar
  • 1,248
  • 9
  • 17
1
vote
3 answers

When using getDerivedStateFromProps (React) getting error: Cannot read property 'setState' of null

So just learned that componentWillReceiveProps is deprecated and we need to use getDerivedStateFromProps lifecycle method now. https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops I'm using it as such below: class Main…
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529
0
votes
1 answer

Can't perform a react state update from a props with a searchbar using getderivedstatefromprops

I was just wondering if you could help me on this one. I want to update the state from a props using getderivedstatefromprops. But the thing is that I also have a search bar in this file which is using the local state to do research. My props are…
0
votes
1 answer

how to use getDerivedStateFromProps instead of componentWillReceiveProps in React

I like to update my code to use getDerivedStateFromProps instead of componentWillReceiveProps as the I am receiving deprecated error. The component is receiving a date prop and every time the date is changed in need to run the getList with the new…
0
votes
1 answer

getDerivedStateFromProps not updating state?

I have this scenario: static getDerivedStateFromProps(nextProps, prevState) { if(...) { console.log("A") return { a: true } } console.log("B") return { …
Ferus
  • 1,080
  • 3
  • 12
  • 17
0
votes
1 answer

Set State into getDerivedStateFromProps (set state, React)

Into class Component I got: state = { user: {} } componentWillMount() { firebaseAuth.onAuthStateChanged((user) => { if(user) { this.setState({ user: { id: user.uid, …
4est
  • 3,010
  • 6
  • 41
  • 63
0
votes
0 answers

How do I update data with stateState without calling getDerivedStateFromProps?

I'm building a kind of e-commerce app. As we know, every e-commerce app has wish list feature. In my product list, there's a like button on the card item. When user click it, it calls an API to set isLiked property to true (boolean) and updated the…
0
votes
0 answers

getDerivedStateFromProps called everytime when local state change

Currently I am setting ownUpdate: true state when we are changing state from same component and check it in getDerivedStateFromProps accordingly. Below is the code I am using in my component. Can anyone suggest better way of doing this. Thanks in…
Vaibhav Agarwal
  • 238
  • 5
  • 19
0
votes
1 answer

How to skip the update of state inside getDerivedStatefromProps() in childComponent only for the first time of rendering the childComponent

Hi I'm new to Reactjs. I want {this.state.data} of the below childComponent to be incremented 5 on each time this component is getting loaded. So i'm doing that under the static method getDerivedStateFromProps(props,state) But for the first…
Ela
  • 91
  • 6
0
votes
1 answer

Will derived state be allowed for React Concurrent Mode?

Today we can use getDerivedStateFromProps for class components and state updates during render phase for React Hooks/function components to create derived state, if needed. I am especially curious about the React Hook variant: Will this be allowed…
0
votes
2 answers

I am not being able to stop using componentWIllReceiveProps / getDerivedStateFromProps

I'm new to React (and programming world actually) and I just found that we shouldn't need to use componentWIllReceiveProps / getDerivedStateFromProps at all, but i have this issue: I have a parent component, LoginForm, in which I fetch my API and…
0
votes
2 answers

Is there a scenario where getDerivedStateFromProps is mandatory?

As stated in the React docs, this API should be used sparingly because simpler alternatives can be used to achieve the same result. This is what I understand and totally agree with. However, as a standalone API, there should be some scenario where…
Zhengquan Bai
  • 1,057
  • 3
  • 14
  • 31
0
votes
0 answers

How to replace componentWillReceiveProps with getDerivedStateFromProps

I read and tried so hard, but still could not find a way to successfully replace componentWillReceiveProps with getDerivedStateFromProps. Here is my code, componentWillReceiveProps one is working fine. but the getDerivedStateFromProps which I tried…
fairy
  • 9
  • 2