Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Sign InSign Up

Softans

Softans Logo Softans Logo
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
Home/ Questions/Q 1327
Anonymous
Anonymous
Asked: February 2, 20222022-02-02T10:06:49+00:00 2022-02-02T10:06:49+00:00

Retrieving latest state from functional child component using callbacks

Coming from Vue and diving into React I seem to struggle with the concept of the hooks / component lifecycle and data flow. Where in Vue I could solve my issues using v-model in React I struggle to do so. Basically:

What I intend to do is : have a parent component which is a form. This component will have several child components where each are fragments of the form data. Each child component manages its own state. The parent component has a submit button that should be able to retrieve the values from all child components.

In a nutshell, my approach is: have a functional component to manage part of said form using state hooks. This “form fragment”-component can broadcast a change event broadcastChange() containing the updated values of its inputs. In the parent I have a submit button which invokes this broadcastChange() event from the child using a ref.

The problem I am running into is that I am always getting the default values of the child state. In the below example, I’d always be getting foo for property inputValue. If I were to add a submit button inside the child component to invoke broadcastChange() directly, I do get the latest values.

What am I overlooking here ? Also, if this is not the React way to manage this two-way communication, I’d gladly hear about alternatives.

Parent code:

function App() {
    const getChildChangesFn = useRef( null );
    const submitForm = e => {
        e.nativeEvent.preventDefault();
        getChildChangesFn.current(); // request changes from child component
    };
    const handleChange = data => {
        console.log( data ); // will always list { inputValue: "foo" }
    };
    return (
        <form>
            <child getChangeFn={ getChildChangesFn } onChange={ handleChange } />
            <button type="submit" onClick={ () => submitForm() }>Save</button>
        </form>
    );
}

Child code:

export default function Child( props ) {
    const [ inputValue, setInputValue ] = useState( "foo" );

    useEffect(() => {
        // invoke inner broadcastChange function when getChangeFn is triggered by parent
        props.getChangeFn.current = broadcastChange;
    }, []);

    const broadcastChange = () => {
        props.onChange({ inputValue });
    };

    render (
        <fieldset>
            <input
                type="text"
                value={ inputValue }
                onChange={ e => setInputValue( e.target.value ) }
            />
        </fieldset>
    );
}
react.js
  • 0
  • 0 0 Answers
  • 22 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • Popular
  • Answers
  • Ghulam Nabi

    Why are the British confused about us calling bread rolls ...

    • 5 Answers
  • Jerry

    Add file to native target programmatically via tuist/XcodeProj

    • 4 Answers
  • Ghulam Nabi

    Is this statement, “i see him last night” can be ...

    • 4 Answers
  • Ghulam Nabi
    Ghulam Nabi added an answer To resolve the NullPointerException, you need to identify the variable… March 15, 2023 at 8:25 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can replace the PnP code in your Azure Function… February 13, 2023 at 7:11 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can use the $match stage in the aggregate pipeline… February 10, 2023 at 6:20 am

Trending Tags

android c++ cypress flutter java javascript python selenium testng webdriver

Top Members

Robert

Robert

  • 3 Questions
  • 1k Points
Luci

Luci

  • 5 Questions
  • 1k Points
Kevin O Brien

Kevin O Brien

  • 2 Questions
  • 1k Points

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Softans

Softans is a social questions & Answers Engine which will help you establish your community and connect with other people.

About Us

  • Blog
  • Jobs
  • About Us
  • Meet The Team
  • Contact Us

Legal Stuff

Help

Follow

© 2021 Softans. All Rights Reserved
With Love by Softans.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.