【问题标题】:React using refs for child components对子组件使用 refs 做出反应
【发布时间】:2020-08-03 19:33:49
【问题描述】:

我想从父组件内部的子组件调用一个函数。我知道我可以使用 React createRef 作为解决方案。到目前为止,我尝试过,但 current 的值仍然为空。也许我忘记了什么,但我无法弄清楚。如果我可以通过其他方式修复它也很好。

我的例子:

import * as React from 'react';
import FormComponent from "./FormComponent";
import CalendarComponent from "./CalendarComponent";
import LinearProgress from '@material-ui/core/LinearProgress';
import {connect} from "react-redux";
import DialogComponent from "../../../shared/modules/dialog/components/DialogComponent";

class WeeklyTimeTrackingComponent extends React.Component {

  constructor(props) {
    super(props);

    this.state = {
      dialogOpen: false
    }

    this.formComponentRef = React.createRef();

    this.openDialog = this.openDialog.bind(this);
    this.handleClose = this.handleClose.bind(this);
    this.handleSave = this.handleSave.bind(this);

  }

  openDialog = () => {
    this.setState({dialogOpen: true});
  }

  handleClose() {
    this.setState({
      dialogOpen: false
    });
  }

  handleSave() {
    console.log(this.formComponentRef.current, "COMPONENTS REFS");
  }

  render(){
    return (
      <div className="weekly-timetracking">
        {this.props.isFetching && (
          <LinearProgress />
        )}
        <CalendarComponent />
        <DialogComponent
          open={this.state.dialogOpen}
          onHandleClose={this.handleClose}
          onHandleSave={this.handleSave}
          title="Add new time registration"
          buttonText="Save">
          <FormComponent ref={this.formComponentRef} />
        </DialogComponent>
        <button
          className="btn btn-primary"
          onClick={this.openDialog}
        >Add project</button>
      </div>

    )
  }
}

const mapStateToProps = state => {
  return {
    isFetching: state.timeTracking.isFetching || false
  };
};

export default connect(mapStateToProps)(WeeklyTimeTrackingComponent);

【问题讨论】:

    标签: javascript reactjs react-ref


    【解决方案1】:

    您可以将所需的方法传递给子组件并从那里调用它,而不是传递 ref,例如:

    class Parent extends Component {
      example() {
        console.log("test");
      }
    
      render() {
        return <Child func={this.example.bind(this)} />
      }
    }
    class Child extends Component {
      render() {
        return <button onClick={this.props.func}>Click me</button>
      }
    }
    

    使用函数组件也一样

    const Parent = () => {
      const example = () => console.log("test");
    
      return <Child func={example} />
    }
    const Child = ({func}) => {
      return <button onClick={func}>Click me</button>
    }
    

    【讨论】:

    • 我正在重用子组件,子组件保留了我想从父组件调用的函数
    • react 中的数据不应该双向流动,它不是为这种方式工作而构建的。如果您想根据子组件中的某些事件在父组件中执行某些操作,则应该传递要从子组件调用的函数,而不是相反。如果 Child 中的某些内容应该根据 Parent 进行更改,您应该将其作为来自 Parent 的道具传递。你能详细说明你想要达到的目标吗?
    • 我正在尝试重新使用表单组件。这次是从对话框内部,所以我想在从 DialogComponent 调用 handleSave 方法后保存表单。保存表单的方法在FormComponent里面
    • 因此您可以将handleSave 作为道具传递给FormComponent,并从FormComponent 调用它。例如。 &lt;FormComponent onSubmit={this.handleSave} /&gt;。 FormComponent 可以将其所有数据传递给该方法,以便您可以在handleSave 方法中使用它们
    • 也许我不明白你的答案,但 handleSave 是来自 DialogComponent 的回调函数。因此,当调用那个时,我知道在 WeeklyTimeTrackingComponent 内单击保存按钮的时间。实际的保存方法在我想保留的 FormComponent 中。
    猜你喜欢
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 2022-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多