1

Below is my code.

CHILD COMPONENT

  const onSave = () => {
    var val = value
    props.onSave();
  }

<div onClick={() => { onSave() }}>
            <CheckButton variant="contained" text-align='center' />
          </div>

PARENT COMPONENT

export const Card = (props) => {
    const onSave = (val) => { [I WANT TO ACCESS val within here] }
}


<TextInputContainer onSave={() => onSave()} />

Is there any way I can access that variable val inside the parent component? The code is truncated big-time. The actual code uses Redux.

arsdever
  • 1,111
  • 1
  • 11
  • 32
abiodunt
  • 9
  • 5
  • Look where the value is coming from. If it is coming from a prop, lift it upwards to the global redux state or to parent. – Easwar Nov 28 '19 at 18:13
  • Does this answer your question? [Pass props to parent component in React.js](https://stackoverflow.com/questions/22639534/pass-props-to-parent-component-in-react-js) – Liastre Nov 28 '19 at 18:24

1 Answers1

0

The createRef of react helps u to access the value of a react component:

Reference (https://pt-br.reactjs.org/docs/hooks-reference.html)

constructor () {this.CheckButtonRef = createRef()}
onSave = () => {props.onSave(this.CheckButtonRef.current.value)}
<CheckButton ref = {this.CheckButtonRef}/>
Zinho
  • 135
  • 9