裏面我有一個這樣的組件:反應執行方法的成分
import React, { Component } from 'react';
class MyComponent extends Component {
constructor(props, context) {
super(props, context);
this.state = {
isActive: false,
}
}
showMyComponent() {
this.setState({
isActive: true,
});
}
hideMyComponent() {
this.setState({
isActive: false,
});
}
render() {
return (
<div>
<h1>Compoent Here</h1>
</div>
);
}
}
export default MyComponent;
現在,我的index.js我加入幾個組件。
...
<Header />
<Nave />
我現在可以做這樣的事情在這裏:
MyComponent.showMyComponent();
就像你通常調用一個函數?
如果不是,這是如何完成的?
好像應該有不同的方法來此。你想達到什麼目的? –
我不認爲這會起作用,您將不得不在類實例上調用該方法。 'MyComponent.showMyComponent'只能用於靜態方法,但是你的方法不是靜態的,而且也不是,因爲那樣它就無法訪問狀態。 –
我歐米同意,請告訴我們的背景是什麼 –