[英]How to fix '_this4.props.handleExport' error in React
I use a handleExport
function in the parent component set in a child under child component. 我在子组件下的子组件中的父组件集中使用handleExport
函数。 However, the error below occurs and that would not work well. 但是,出现以下错误,并且无法正常工作。
_this4.props._handleExport is not a function _this4.props._handleExport不是函数
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}
/>
)
}
}
You can bubble up requests through props passed into child containers like so. 您可以像这样通过传递到子容器中的道具增加请求。
// --- 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>
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.