Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

95
Vistas
How to have React re-render a functional component when prop changes

In my app, i am passing some weather data to a component as a prop and I want to have the color of the temp update based on that value. Here is what I did. It seems that react does not always re-render when a prop is changed. How can I make sure that this gets updated every time the prop changes?

const Current = (props) => {

const { weather } = props

const [color, setColor] = useState([])

useEffect(() => {
    setColor(tempScheme[weather.current.temp_f])
}, [weather, color])

return (
    <Container>
        <CardContent>
            <Header>
                <Typography variant='h6'>Currently</Typography>
                <Typography variant='h6'> {formatDate(props.weather.location.localtime)} </Typography>
            </Header>
            <Weather>
                <Typography variant='h5'> {props.weather.current.condition.text} </Typography>
                <Typography variant='h2' style={{"color": `rgb(${color})`}}> {Math.round(props.weather.current.temp_f)}&deg; </Typography>
            </Weather>
            <Location>
                <Typography variant='body1'> {props.weather.location.name}, {props.weather.location.region}</Typography>
                <Image src={props.weather.current.condition.icon} />
            </Location>
        </CardContent>
    </Container>
)

}e here

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You're mixing up a few concepts here, which is breaking the implementation. On the one hand, you're passing the weather object (with color on it) as a prop, but you're also storing/referencing this in local state, and using useEffect to set this internal instance of color. In other words, you are not reacting to the changes to color from the parent component. In addition, react won't re-render if a key/value changes on an object in a parent component, only if the object itself changes. what you want to do is move your color state, and the useEffect up one level, and also adjust the dependency array of the useEffect so it subscribes to the right change, so

const [color, setColor] = useState(tempScheme[weather.current.temp_f] 

and

useEffect(() => {
 setColor(tempScheme[weather.current.temp_f])
}, [weather.current.temp_f])

then pass this into the child as a color prop

<Current color={color} />

Then to consume within the component itself you can just do

const current = ({ color }) => ...restOfTheComponent 

alternatively, you can probably simplify this by just removing the local state and use effect, and just passing the color directly like this

<Current color={weather.current.temp_f} />
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda