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

401
Views
How to update Parent component from Child component in React (Functional)

I have the following functional component called RoomManagement. On the first render, fetchRooms gets called, which in turn initially set the state variables with data from a database.

I have a child component called RoomManagementModal, where I pass both my setLoading and fetchRooms function.

I'm having trouble re-rendering the parent component. I click a button in the child component (this adds something to the database). So I'm trying to then get the new data by calling props.fetch() once again but this time from the child, which I thought would trigger a state change on the parent, but it doesn't seem that way.

I can't seem to find a way to kind of force the parent component to render again, if that is possible

function RoomManagement() {

    const [rooms, setRooms] = useState([]);
    const [unavailableRooms, setUnavailableRooms] = useState();
    const [loading, setLoading] = useState(true);

    const fetchRooms = async () => {
        await getAllRooms().then((res) => {
            res.json().then(data => setRooms(data))
        });
        await getAllRoomsUnavailable().then((res) => {
            res.json().then(data => setUnavailableRooms(data[0]))
        })
        setLoading(false)
    }
    
    useEffect(() => {
        fetchRooms();
    }, []);
    
    return (.....
   <RoomManagementModal setLoading={setLoading} fetch={fetchRooms}/>
)

}
export default RoomManagement;
<Button
          onClick={() => {
            if (form.room_id && form.start && form.end) {
              createRoomUnavailable(form).then((res) => {
                if (res.status != 200 && res.status != 201) {
                  res.text().then((e) => setError(e));
                } else {
                  props.fetch()
                }
              });
            } else {
              setError("Missing required Room Id or date time.");
            }
          }}
        >
          Submit
        </Button>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Parent component

1.Declare a state and a setter for the title or slug 2.Define a function to update the state 3.Pass that function down as props to the child component

Child Component

1.Define a function that handles and calls the function passed as props 2.from the parent component

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!