Reactjs 从子类组件重新呈现/刷新父类组件
我有父类和子类组件,希望从子组件更新父组件。但我得到了以下错误。 实际的代码文件太大,这就是为什么我没有提到整个代码,而是试图解释我是如何做的 多谢各位 父组件:Reactjs 从子类组件重新呈现/刷新父类组件,reactjs,react-native,Reactjs,React Native,我有父类和子类组件,希望从子组件更新父组件。但我得到了以下错误。 实际的代码文件太大,这就是为什么我没有提到整个代码,而是试图解释我是如何做的 多谢各位 父组件: export class parent extends React.Component { ..... componentDidMount() { A(); } A() { this.B(); } B() { ....... } render() { return ( <child
export class parent extends React.Component {
.....
componentDidMount() {
A();
}
A() {
this.B();
}
B() {
.......
}
render() {
return (
<child a = {this.A} />
)}
}
export class parent extends React.Component {
componentDidMount() {
C();
}
C() {
.....(other functionality)
this.props.a();
}
}
export class parent extends React.Component {
.....
B() { // define it like this here
}
A = () => { // define `A` as an arrow function as you pass it like `this.A` instead of `() => this.A()`
this.B();
}
render() {
return (
<child a = {this.A} />
)
}
}
错误:未捕获类型错误:此.B不是函数当您通过链接传递它时,您的函数a(){}
丢失了上下文
要修复它,请使用类属性声明和arror函数
类MyComponent扩展了React.Component{
A=()=>{
这个;
}
// ...
}
父组件中的定义的函数类型B()
不正确
父组件:
export class parent extends React.Component {
.....
componentDidMount() {
A();
}
A() {
this.B();
}
B() {
.......
}
render() {
return (
<child a = {this.A} />
)}
}
export class parent extends React.Component {
componentDidMount() {
C();
}
C() {
.....(other functionality)
this.props.a();
}
}
export class parent extends React.Component {
.....
B() { // define it like this here
}
A = () => { // define `A` as an arrow function as you pass it like `this.A` instead of `() => this.A()`
this.B();
}
render() {
return (
<child a = {this.A} />
)
}
}
导出类父类扩展React.Component{
.....
B(){//定义如下
}
A=()=>{//A`定义为一个箭头函数,您可以像这样传递它,而不是像这样传递它`
这个;
}
render(){
返回(
)
}
}
您正在调用函数B()代码>你的函数定义如B(){}
在哪里?对不起,我编辑了它。你可以看到功能的定义