qq_笑_17
2021-04-04 15:11:24
我有2個(gè)組成部分:儀表板-應(yīng)用程序的入口點(diǎn)帖子在儀表板中,componentDidMount中有一個(gè)API調(diào)用。在“帖子”組件中,收到帖子后,我導(dǎo)航至“儀表板”。是否可以檢測應(yīng)用是否已從“帖子”導(dǎo)航到“儀表板”并刪除componentDidMount中的API調(diào)用。檢查以下代碼:// Dashboard.js componentDidMount() { this.handleApiCall(); // default axios get request// Here I need to detect if the user was navigated to Dashboard from Posts or other component}// Posts.js handleNavigation = () => { this.setState({ isOpen: false, }); this.props.navigation.navigate('Dashboard'); };
1 回答

慕森卡
TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個(gè)贊
嘗試傳遞來自的參數(shù) Posts.js
// Dashboard.js
componentDidMount() {
const { navigation } = this.props;
const fromPosts = navigation.getParam('fromPosts', false);
if(!fromPosts) {
this.handleApiCall(); // default axios get request
}
}
// Posts.js
handleNavigation = () => {
this.setState({
isOpen: false,
});
this.props.navigation.navigate('Dashboard', {fromPosts: true});
};
添加回答
舉報(bào)
0/150
提交
取消