简体   繁体   English

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

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

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?有什么方法可以访问父组件内的变量val吗? The code is truncated big-time.代码被大量截断。 The actual code uses Redux.实际代码使用 Redux。

The createRef of react helps u to access the value of a react component: react 的createRef帮助你访问 react 组件的值:

Reference ( https://pt-br.reactjs.org/docs/hooks-reference.html )参考( 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.

相关问题 将带有参数的函数传递给子组件并将返回值发送回父组件以存储在 reactjs 中的父状态 - Passing a function with a parameter to child component and sending return value back to parent to be stored in parent state in reactjs 将数据从子级传递到父级组件-反应-通过回调函数 - passing data from child to parent component - react - via callback function 在react-redux中将函数从子组件传递给父组件 - Passing function from child to parent component in react-redux 通过 React 功能组件将 function 从父级传递给子级 - Passing down function from parent to child through React functional component React:无法通过将函数传递给更新它的子组件来更新父状态 - React: unable to update parent state by passing function to child component that updates it React父级组件道具未传递给子级组件 - React parent component props are not passing to child component React Native-Redux-将值从子级传递给父级中的函数 - React Native - Redux - Passing Value from child to function in Parent React Native中如何从子组件调用父函数并将子组件的参数传递给父组件? - How can calling parent function from child component and passing parameters from child component to parent component in React Native? React,将状态从子组件传递给父组件 - React, passing state from a Child component to the Parent 在React中将子组件的值传递给父组件 - Pass value of child component to parent component in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM