Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
555 views
in Technique[技术] by (71.8m points)

javascript - 将子组件中函数中存储的值传递给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。)   ask by abiodunt translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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}/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...