import React from 'react'
import {
StackNavigator
} from 'react-navigation'
import Home from './Home'
import Detail from './Detail'
import MyIcon from './MyIcon'
export default StackNavigator({
Home: {
screen: Home,
navigationOptions: {
title: 'Foo',
headerRight: (<MyIcon dispatch={}/>), //<- Here
}
},
Detail: {
screen: Detail,
navigationOptions: {},
},
})
I want to pass dispatch function into HeaderComponent like headerRight in React Navigation Option setting. How can I do this?
You need to call
your dispatch
function in headerRight
and set when your component
is mounted
using this.props.navigation.setParams
import React from 'react'
import {
StackNavigator
} from 'react-navigation'
import Home from './Home'
import Detail from './Detail'
import MyIcon from './MyIcon'
export default StackNavigator({
Home: {
screen: Home,
navigationOptions: ({ navigation }) => {
title: 'Foo',
headerRight: (<MyIcon
onPress={ () => navigation.state.params.dispatch() } />)
// calling dispatch when headerRight icon is press
}
},
Detail: {
screen: Detail,
navigationOptions: {},
},
})
Set dispatch
function in your component
...
//setting dispatch function to headerRight in your component
componentDidMount() {
this.props.navigation.setParams({
dispatch: this.dispatch.bind(this)
});
}
dispatch() {
// your code
}
I hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With