Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
react - the prop value remain unchanged after parents update corresponding state

There are 2 components in my Application, which are Button and Input enter image description here

After I click Click to update state button, the value of input with value May should be changed to May New, but I find that it doesn't happen.

When I check the Input component, the useEffect does not fire.

function Input({ inputValue }) {
  // the useEffect does not fire
  useEffect(() => {
    console.log(inputValue);
  }, [inputValue]);
  ...
}

How can I solve it?

App.js

import "./styles.css";

import React, { useState } from "react";

import Input from "./Input";
import Button from "./Button";

export default function App() {
  const [people, setPeople] = useState([
    {
      id: 0,
      name: "May"
    },
    {
      id: 1,
      name: "John"
    }
  ]);
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      {people.map((pp) => {
        return <Input key={pp.id} inputValue={pp} />;
      })}
      <Button people={people} setPeople={setPeople} />
    </div>
  );
}

Button.js

import React from "react";

function Button({ people, setPeople }) {
  return (
    <button
      onClick={() => {
        let peopleTemp = people;
        peopleTemp[0]["name"] = "May New";
        setPeople(people);
      }}
    >
      Click to update state
    </button>
  );
}

export default Button;

Input.js

import React, { useEffect } from "react";

function Input({ inputValue }) {
  useEffect(() => {
    console.log(inputValue);
  }, [inputValue]);

  return <input readOnly defaultValue={inputValue.name} />;
}

export default Input;

Codesandbox
https://codesandbox.io/s/wizardly-liskov-k8kxin?file=/src/Button.js

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You should do what Gabriele suggested to solve the issue in the console, but you could run in another problem since defaultValue is used for initialization and it won't change even mutating the state. To hack around this you can set a key on the input like: <input key={inputValue.name} readOnly defaultValue={inputValue.name} />;

about 4 years ago · Juan Pablo Isaza Report

0

Remove the useEffect in the Input component. It will work. there is no need of using useEffect.

about 4 years ago · Juan Pablo Isaza Report

0

That is because you are mutating the state.

use

onClick = {() => {
    const updatedPeople = people.map((person, index) => {
      // here you check if the person is the one you want to update
      if (index === 0) {
        return { ...person,
          name: 'May New'
        };
      }
      return person;
    })
    setPeople(updatedPeople);
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!