Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating component's state using props with functional components

I am trying to update the state of my component using props that I get from the parent component, but I get the following error message:

Too many re-renders. React limits the number of renders to prevent an infinite loop.

I want the local state to update if the prop changes. The similar posts (Updating component's state using props, Updating state with props on React child component, Updating component's state using props) did not fixed it for me.

import React, {useState} from "react"


const HomeWorld = (props) => {
    const [planetData, setPlanetData] = useState([]);
    if(props.Selected === true){
        setPlanetData(props.Planet)
        console.log(planetData)
    }


    return(
        <h1>hi i am your starship, type: {planetData}</h1>
    )
}

export default HomeWorld
like image 781
Jonas.D Avatar asked Sep 24 '19 13:09

Jonas.D


People also ask

How do you update a state in a functional component?

Updating the State The state update function is just a regular function. It's used in the onClick handler to replace the current state value. React's internal handling of state values ensures your component will then be re-rendered. useState() will supply the new value, causing the state change to be effected.

How do you change state when props change?

To update state when props change in React: Pass the props as dependencies to the useEffect hook. Every time the props change, the logic in useEffect is reran.

How do you Rerender a functional component in React when props changes?

React components automatically re-render whenever there is a change in their state or props. A simple update of the state, from anywhere in the code, causes all the User Interface (UI) elements to be re-rendered automatically. However, there may be cases where the render() method depends on some other data.

How do you update state immediately in React functional component?

To update state in React components, we'll use either the this. setState function or the updater function returned by the React. useState() Hook in class and function components, respectively.


1 Answers

You need to use the useEffect hook to run it only once.

import { useEffect }  from 'react'

... 

const HomeWorld = (props) => {
    const [planetData, setPlanetData] = useState([]);

    useEffect(() => {
        if(props.Selected === true){
            setPlanetData(props.Planet)
            console.log(planetData)
        }
    }, [props.Selected, props.Planet, setPlanetData]) // This will only run when one of those variables change

    return(
        <h1>hi i am your starship, type: {planetData}</h1>
    )
}

Please notice that if props.Selected or props.Planet change, it will re run the effect.

Why Do I Get This Error ?

Too many re-renders. React limits the number of renders to prevent an infinite loop.

What is happening here is that when your component renders, it runs everything in the function, calling setPlanetData wich will rerender the component, calling everything inside the function again (setPlanetData again) and making a infinite loop.

like image 200
Vencovsky Avatar answered Sep 30 '22 22:09

Vencovsky