javascript - 如何修复 React 中的 '_this4.props.handleExport' 错误

标签 javascript reactjs

我在子组件下的子组件中设置的父组件中使用了handleExport函数。但是,会出现以下错误,并且效果不佳。

_this4.props._handleExport is not a function

src/components/PrimaryItem.js

export default class PrimaryItem extends Component {
  render() {
    return (
      <Stage
        ref={ref => {
          this.props._handleExport(ref);
        }}
      >
      </Stage>
    );
  }
}

src/components/PrimaryContainer.js

import PrimaryItem from "./PrimaryItem";


<PrimaryContainer
  _handleExport={this._handleExport}
/>

src.App.js

import PrimaryItem from "./components/PrimaryContainer";

export default class App extends component {
  _handleExport = ref => {
    if (ref) {
      this.refStage = ref;
    }
  };
  render() {

  return(
  <PrimaryContainer 
    _handleExport={this._handleExport}
  />
  )
}
}

最佳答案

您可以像这样通过传递到子容器的 props 来冒泡请求。

// --- parent.js

import React, { Component, Fragment } from "react";
import { ChildComponent } from './containers/child'

export class ParentContainer extends Component {

  handleUpdate = () => {
    // whatever you want to do here
    console.log('I have been clicked from ChildContainer')
  }

  render() {
    return (
      <Fragment>
        <ChildComponent onUpdate={this.handleUpdate} />
      </Fragment>
    );
  }
}

// --- child.js

import React, { Component, Fragment } from "react";

export class ChildComponent extends Component {

  constructor(){
    super()
    console.log(this.props) // will show you the props you have access to pass into components.
  }

  render() {
    return (
      <button onClick={this.props.onUpdate}></button>
    );
  }
}

关于javascript - 如何修复 React 中的 '_this4.props.handleExport' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53862303/

相关文章:

Javascript检测它是从哪个主机加载的

javascript - 如何将 MySQL 数据库连接到 ReactJS 应用程序?

reactjs - Webpack - url-loader 无法解析 jpg

javascript - Jquery width() 通过迭代?

javascript - 在laravel中的同一页面返回json结果

javascript:为什么要 "sign-prefix"一个变量?

javascript - 如何从 defaultProps 迁移到默认参数

reactjs - Formik中具有初始值的复选框不切换,需要处理选中/默认选中

reactjs - Reactjs 中的 {...this.props} 是什么意思

javascript - 如何使用默认值加载可能未定义的模板变量?