繁体   English   中英

将存储在子组件内函数中的值传递给 React 中的父组件

[英]Passing a value stored in a function within a child component to a parent in React

下面是我的代码。

子组件

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

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

父组件

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


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

有什么方法可以访问父组件内的变量val吗? 代码被大量截断。 实际代码使用 Redux。

react 的createRef帮助你访问 react 组件的值:

参考( https://pt-br.reactjs.org/docs/hooks-reference.html

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM