[英]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 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.