[英]How can I use the function of one component in another component? (react)
I want to use a function setCount()
in the class Footer
in another component.我想在另一个组件的
Footer
类中使用函数setCount()
。 But I get an error that I do not know why.但是我收到一个错误,我不知道为什么。 class Footer:
类页脚:
export default class Footer extends Component {
setCount()
{
//code
}
render() {
return (
<div >
//code
</div>
);
}
}
class TodoItem: TodoItem 类:
import footer from "./Footer"
@observer
class TodoItem extends Component {
ontoggle=()=>{
this.props.todo.toggle();
footer.setCount() //this line get an error
}
render(){
const {todo} =this.props
return(
//code
)
}
}
type error: TypeError: _Footer__WEBPACK_IMPORTED_MODULE_8__.default.setCount is not a function
类型错误:
TypeError: _Footer__WEBPACK_IMPORTED_MODULE_8__.default.setCount is not a function
How can I fix it?我该如何解决?
You can make setCount
method as static in order to make it work.您可以将
setCount
方法设为静态以使其工作。 However, a static method cannot access this
keyword.但是,静态方法无法访问
this
关键字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.