我認爲當你說你的意思是隻安裝一次「一旦組件只執行」。
既然你沒有顯示你的代碼,我只能假設你已經使用了lifecycle methods之一:componentWillMount
| componentDidMount
這些方法只在組件安裝時觸發一次。給定你的路由配置,每當你切換到不同的URL,因爲它使用相同的組件,它不會再次卸載和掛載(因此你的加載邏輯只觸發一次),但如果它的道具已經改變,只需重新渲染。這就是爲什麼你應該插入一個生命週期方法,這個方法在每個支持改變時觸發(比如componentWillReceiveProps
)。
試試這個:
class TVPage extends Component {
constructor(props) {
super(props);
}
componentWillMount() {
// Load your data/state (initial)
this.props.loadMyData(this.props.whatever.data);
}
componentWillReceiveProps(nextProps) {
if (this.props.whatever.myStatus !== nextProps.whatever.myStatus) {
// Load your data/state (any page change)
nextProps.loadMyData(nextProps.whatever.data);
}
}
render() {
// Render whatever you want here
}
}
componentWillMount
將觸發安裝(初始負載),並componentWillReceiveProps
將至少你的道具更改每次觸發。
你究竟想要做什麼,需要組件重新執行? – erichardson30
每個頁面使用相同類型的組件,並根據url –