send a value from the child component to the parent component reactjs

Leticia Fatima

I have a screen with a TAB, that TAB is a component and within the TAB there is another component called FORM.

hierarchy:

  • SCREEN
    • TAB
      • FORM

in this hierarchy, I want to pass the NAME value from the FORM component to the TAB component and then from the TAB component to the SCREEN component.

Code Screen:

export default function Screen() {
  return (
    <>
      <TabProfile me={me} />
    </>
  );
}

Code TAB:

export default function TabProfile({ me }) {
  return (
    <div className={classes.root}>
      <div className={classes.demo1}>
        <AntTabs value={value} onChange={handleChange} aria-label="ant example">
          <AntTab label="Config" />
        </AntTabs>
      </div>
      <div>
        <TabPanel value={value} index={0}>
          <Config me={!me ? [] : me} />
        </TabPanel>
      </div>
    </div>
  );
}

Code FORM:

export default function Config({ me }) {
  return (
    <>
      <BoxFormGrid>
        <Label>Name</Label>
        <Input type="text" value={name} />
      </BoxFormGrid>
    </>
  );
}
David

You can do this by having a state in your top component. Then you pass the state update function down twice:

import { useState } from 'react'

export default function Screen() {
  const [ name, setName ] = useState('me') // 'me' is the start value

  return (
    <>
      <TabProfile me={name} setName={setName} />
    </>
  );
}
export default function TabProfile({ me, setName }) {
  return (...
          <Config me={!me ? [] : me} setName={setName} />
  ...
  );
}
export default function Config({ me, setName }) {
  return (
    <>
      <BoxFormGrid>
        <Label>Name</Label>
        <Input type="text" value={me} onChange={event => setName(event.target.value)} />
      </BoxFormGrid>
    </>
  );
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to send data from child component to parent component in reactjs

Send value from child component to parent in React

Send props from parent to child and update them in child component (ReactJs)

How to send the value of variable from a child component to the parent component?

How to send a value to parent component from child component based on condition?

How to get the value from one component(Child Component) to another component (Parent Component) in reactjs?

Child component does not set the initial value passed from the parent: ReactJS

Reactjs getting a state value from a child (with hookReducer) to parent component

How to pass a value from child to parent component in reactjs

How to send value from child component to parent?(functional components)

ReactJS modify parent state from child component

react send data from child component to parent

Am Unable to pass the value from Child to Parent component(FUNCTIONAL COMPONENT) in ReactJS

ReactJS how to pass child component title from parent component

How to pass Array from Parent component to Child component in reactjs

How to force update child component from parent component in reactjs

How to track state of parent component from child component in ReactJs?

How to send data from a child component to parent component in React

How to send styles from parent component to child component?

How to send data from parent component to child component in AngularJs

send form values from child function component to parent class component

send form data from child component to parent component

Passing the value from the child to parent component and then to the child component again

How can I send data from a child to a parent functional component in ReactJS?

Reactjs getting a state value from a child component

React hooks - send updated value from child to parent component when using onChange and prop of child toghether

How to pass value from child to parent component

Child component not update value from Parent

How to pass a value from a child to a parent component?