javascript - React-Navigation: How to pass data between tabs in TabNavigation? - Stack Overflow
I Have two tabs in tabNavigation
const HomeStack = createStackNavigator({
Home: HomeView
});
const SettingStack = createStackNavigator({
Setting: SettingView
});
const TabNavigator = createBottomTabNavigator({
Home: HomeStack,
Setting: SettingStack
});
So I want to switch tab from HomeView
to SettingView
// IN HOME VIEW
this.props.navigation.navigate('Setting', {
someFlag: true,
data: "SET"
});
via button action and want to send some data as below.
// IN SETTING VIEW
const { navigation } = this.props;
const openPCPSchedule = navigation.getParam("someFlag", false);
const data = navigation.getParam("data", null);
console.log("NAVI PARAMS: ", openPCPSchedule); // false
console.log("NAVI data: ", data); // null
Getting false
and null
at SettingView
, Need a correct way to get data from one to tab to other?
I Have two tabs in tabNavigation
const HomeStack = createStackNavigator({
Home: HomeView
});
const SettingStack = createStackNavigator({
Setting: SettingView
});
const TabNavigator = createBottomTabNavigator({
Home: HomeStack,
Setting: SettingStack
});
So I want to switch tab from HomeView
to SettingView
// IN HOME VIEW
this.props.navigation.navigate('Setting', {
someFlag: true,
data: "SET"
});
via button action and want to send some data as below.
// IN SETTING VIEW
const { navigation } = this.props;
const openPCPSchedule = navigation.getParam("someFlag", false);
const data = navigation.getParam("data", null);
console.log("NAVI PARAMS: ", openPCPSchedule); // false
console.log("NAVI data: ", data); // null
Getting false
and null
at SettingView
, Need a correct way to get data from one to tab to other?
3 Answers
Reset to default 2You need to use dangerouslyGetParent()
in SettingView. this.props.navigation.navigate
sends params to the parent, not to the screen.
The code in SettingView will be:
const { navigation } = this.props;
const openPCPSchedule = navigation.dangerouslyGetParent().getParam("someFlag", false);
const data = navigation.dangerouslyGetParent().getParam("data", null);
We get props from previous tab using this.props.navigation.
While passing data add
this.props.navigation.navigate('Setting', {
someFlag: true,
data: "SET"
});
To access above data on Setting screen add following code in ponentDidMount method or in render method
this.props.navigation.state.params.someFlag
// You can access someFlag as true here
You can use screenProps
ScreenProps
Usage Link Page
- screenProps - Pass down extra options to child screens
//MAIN VIEW
class YourApp extends Component {
render() {
const screenProps = {
someFlag: true,
data: "SET"
}
return (
<TabNavigator screenProps={screenProps} />
)
}
}
export default YourApp
AppRegistry.registerComponent('YourApp', () => YourApp);
// IN SETTING VIEW
class SettingScreen extends Component {
render() {
const { navigation, screenProps } = this.props
return (
<View>
<Text>{screenProps.someFlag}</Text>
<Text>{screenProps.data}</Text>
</View>
)
}
}
- 软件定义:英特尔携VMware重塑数据中心
- Computex 2012:Windows 8将怎样改变PC行业
- 2012年NEC云时代平台软件全国巡展启动
- Unable to create a azure synapse workspace? - Stack Overflow
- TypeScript Error (ts2345) when trying to collect property names in array - Stack Overflow
- ST_CONTAINS() giving FALSE even if Point lies within polygon. google-bigquery - Stack Overflow
- node.js - I am getting error when i click on submit button | Discord.JS - Stack Overflow
- swift - How to Add .mlmodel File to Xcode App Playgrounds (.swiftpm) Project? - Stack Overflow
- reactjs - how to fetch headers in middleware in Next js - Stack Overflow
- sql - Merge tables with different timestamps? - Stack Overflow
- c++ - Which option has precendence if I enable and disable FrontEndHeapDebugOptions at the same time? - Stack Overflow
- reactjs - How to deploy Laravel (with react) app on heroku without any error (like 419 error) - Stack Overflow
- javascript - Firebase Auth link - Problem with the Google login, no possibility to change to own project name - Stack Overflow
- php - Laravel Defer on API requests - Stack Overflow
- sockets - PICO WMicropython websocket client can't send to PHP websocket properly - Stack Overflow
- c++ - Member of struct constructed twice in custom constructor? - Stack Overflow
- How to Use GraalVM to Execute JavaScript Code Making REST Calls Using Java? - Stack Overflow