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

236
Views
React error : TypeError: props.globalChange is not a function

I am having a problem in props sending in the same component and the function is not function issue coming. So how to solve the issue?

import React, { useState } from 'react';

export default function Lifting() {
    const [data, setData] = useState(0);
    function globalChange(items) {
        setData({ data: items });
    }
    return (
        <div>
            <h1>Lifting State Up</h1>
            <LiftingChild val={data} onChange={globalChange} />
            <br />
            <LiftingChild val={data} onChange={globalChange} />
        </div>
    )
}

function LiftingChild(props) {
    return (
        <div>
            <input value={props.val} onChange={(e) => { props.globalChange(e.target.value) }} />
        </div>
    )
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It's just a naming issue. When you used the LiftingChild component you add globalChange as the onChange prop but in the LiftingChild component you try to call globalChange that doesn't exist. You need to call it as props.onChange.

<input value={props.val} onChange={(e) => { props.onChange(e.target.value) }} />

EDIT: This exposes that the way the setData was being called is also incorrect. You're setting the new state to be an object but it should just be the number so it can continue to be used at the state of the form.

function globalChange(item) {
        setData(item);
    }

Note that because both child components use the same piece of state, updating one will update both. This could be desirable or not depending on the use-case.

Putting it all together it should look like so:

const { useState } = React;

function Lifting() {
    const [data, setData] = useState(0);
    function globalChange(items) {
        setData(items);
    }
    return (
        <div>
            <h1>Lifting State Up</h1>
            <LiftingChild val={data} onChange={globalChange} />
            <br />
            <LiftingChild val={data} onChange={globalChange} />
        </div>
    )
}

function LiftingChild(props) {
    
    return (
        <div>
            <input value={props.val} onChange={(e) => { props.onChange(e.target.value) }} />
        </div>
    )
}

ReactDOM.render(<Lifting />, document.getElementById('react'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>

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!