How to change header title in react-native-router-flux - react-native

I'm making simple application using react-native, and I am Using react-native-router-flux.
I want to change header component from 'chat' to 'Username' when i'm in nested screen. I tried several ways but I don't know where to put the header component code.
Code for TabBar
<Router titleStyle={styles.navTitle} >
<Scene key="root">
<Scene title="Chat" initial key="root" tabBarPosition='top' tabs tabBarStyle={{ top: 0, backgroundColor: '#ddd' }} tabBarIconContainerStyle={{ borderColor: '#000', borderWidth: 1}} initial>
<Scene key="first" title="First" icon={TabIcon}>
<Scene key="scarlet" component={BlueScreen} hideNavBar initial />
</Scene>
<Scene key="second" title="Second" icon={TabIcon}>
<Scene key="scarlet2" component={GrayScreen} hideNavBar initial />
</Scene>
<Scene key="third" title="Third" icon={TabIcon}>
<Scene key="scarlet3" component={ScarletScreen} hideNavBar initial />
<Scene key="root" component={SecondScene} hideNavBar title="Username" />
</Scene>
</Scene>
</Scene>
when I'm in SecondScene I want my header change from Chat to Username

You can add this code in the Component where you switch the tab and want username as header.
Actions.refresh({ title: 'Username' }).
you can add this line of code as per your Apps flow.
If you are switching to chat you can add
Actions.refresh({ title: 'chat' }).
Hope it helps !

You can change navigation title anytime. For your use when you got the username just run the following code.
this.props.navigation.setParams({
title: "YOUR_USER_NAME",
});
If you get the user name when navigating to current scene it's better you change the title in componentWillMount.
componentWillMount() {
this.props.navigation.setParams({
title: "YOUR_USER_NAME",
});
}

Related

React native router flux authentication flow for tabs as in show accounts tab in case of unauthorize and show profile tab in case of authorize

In my app, the authentication is not the first screen to be shown. The home page contains a list of item that a user can browse through and see the details of an individual item. In order, to subscribe to that item, I need to redirect the user to the accounts tab. In case user is logged in, I want to show the profile tab otherwise the login page which has is part of an auth stack of pages.
So how can I do that using react-native-router-flux, I have tried the following approach as per my understanding and it isn't working. I am new to react-native and struggling with this flow.
If there is another library which can help me to achieve this desired behavior, I would love to try it out.
Routing.tsx
<Router>
<Scene key="root" hideNavBar>
<Tabs key="tabbar" swipeEnabled tabBarStyle={styles.tabBar} showLabel={false} lazy>
<Scene key="homeTab" title="Home" iconName="ios-home" icon={TabIcon} hideNavBar>
<Scene key="home" component={Home} title="Home Page" initial />
<Scene key="details" component={ContentDetail} />
</Scene>
<Scene key="account" component={Splash} hideNavBar title="Profile" iconName="ios-person" icon={TabIcon}>
<Scene key="profileTab" title="Profile" iconName="ios-person" icon={TabIcon} hideNavBar>
<Scene key="profile" component={Profile} initial />
</Scene>
<Scene key="authTab" title="Account" iconName="ios-person" icon={TabIcon} hideNavBar>
<Scene key="login" component={Login} initial />
<Scene key="signup" component={Signup} />
<Scene key="verifyTac" component={VerifyTac} />
</Scene>
</Scene>
<Scene key="settingTab" title="Settings" iconName="ios-settings" icon={TabIcon} hideNavBar>
<Scene key="accInfo" component={AccountInfo} title="Account Info Page" initial />
</Scene>
</Tabs>
<Scene
key="search"
component={Search}
modal
hideNavBar
/>
</Scene>
</Router>
Splash.tsx
class Splash extends React.Component<Props, {}> {
constructor(props: Props) {
super(props);
}
componentDidMount() {
if (this.props.user && this.props.user.accessToken) Actions.replace('profileTab');
else Actions.replace('authTab');
}
render() {
console.log(' Splash props: ', this.props);
return <View></View>
}
}
const mapStateToProps = state => {
return {
user: state.authReducer
}
}
export default connect(mapStateToProps)(Splash);

how to navigate from one scene to another scene in react native router flux?

I am using react native router flux in my react native application.
I created some scenes in Route.
<Router>
<Scene key="root" hideNavBar={true} navigationBarStyle={{ backgroundColor: '#81b71a' }}>
<Scene key="auth">
<Scene key="login" component={LoginForm} title="Please Login" />
</Scene>
<Scene key="main" navigationBarStyle={{ backgroundColor: '#8fff1f' }}>
<Scene
key="mainScene1"
component={EmployeeList}
title="MainScene1"
/>
<Scene key="mainScene2" component={EmployeeCreate} title="MainScene1" />
<Scene key="MainScene3" component={EmployeeEdit} title="Edit Employee" />
</Scene>
</Scene>
</Router>
I want to navigate from mainScene3 to mainScene1 but getting error "Error: There is no route defined for key mainScene1. Must be one of: 'auth','main' "
How to tackle this issue? These two scene are in the same bucket.
Another query - How to navigate from login scene of auth to any scene say mainScene2 ? Is there any special method for that?

React native router flux Reset Stack when change Tab

I word on react-native-router-flux and I have a problem ... I am reset my Stack navigation when I am changing of Tab navigation.
I try with backToInitial but I need to press two times on my tab for reset my stack ... I don't understand why.
My navigation :
-OSU
-Scarlet
-Gray
-VU
-Blue
-Black
So when I am on Blue and I press one time on OSU I when to access at Scarlet not gray
My code :
export default class App extends Component {
render() {
return (
<Router>
<Tabs
key="tabbar"
tabBarStyle={{ backgroundColor: '#FFFFFF' }}
backToInitial={true}
>
<Scene key="osu" title="OSU" icon={TabIcon} type='reset' backToInitial={true}>
<Scene
key="scarlet"
component={ScarletScreen}
title="Scarlet"
/>
<Scene
key="gray"
component={GrayScreen}
title="Gray"
/>
</Scene>
<Scene key="vu" title="VU" icon={TabIcon} backToInitial={true}>
<Scene
key="blue"
component={BlueScreen}
title="Blue"
/>
<Scene
key="black"
component={BlackScreen}
title="Black"
/>
</Scene>
</Tabs>
</Router>
);
}
}

Pass props into TabIcon with React Native Router Flux

I'm using react-native-router-flux in my app and want to pass in paths to png images for my TabIcon components. I know I could just make a different tab icon component for each tab, but these icons would be exactly the same except for the image source and I want to find a DRY way to accomplish my goal. Where in my scenes can I pass in the image path?
Here is my TabIcon component:
const TabIcon = ({ selected, title, image }) => {
const tabStyle = selected ? styles.selected : styles.unselected
return (
<View style={tabStyle}>
<Image
style={{height: 35, width: 35}}
source={require(image)}
/>
</View>
)
}
My scenes:
const Scenes = Actions.create(
<Scene key='root'>
<Scene key='tabbar' tabs={true} tabBarStyle={styles.tabBarStyle}>
<Scene key='tab1' title='Feed' icon={TabIcon}>
<Scene
key='Feed'
component={Feed}
title='Feed'
initial={true}
/>
</Scene>
<Scene key='tab2' title='Create' icon={TabIcon}>
<Scene
key='Create'
component={Create}
title='Create'
/>
</Scene>
<Scene key='tab3' title='Leaderboard' icon={TabIcon}>
<Scene
key='leaderboard'
component={Leaderboard}
title='Leaderboard'
/>
</Scene>
</Scene>
</Scene>
)
EDIT
I have tried passing in the image like so
const Scenes = Actions.create(
<Scene key='root'>
<Scene key='tabbar' tabs={true} tabBarStyle={styles.tabBarStyle}>
//HERE
<Scene key='tab1' title='Feed' icon={TabIcon} image={'../images/feed.png'}>
<Scene
key='matchupsFeed'
component={MatchupsFeed}
title='Feed'
initial={true}
/>
</Scene>
...
...
...
Within my TabIcon component if I console.log(image) it prints out "../images/feed.png" but I get this error in the simulator:
Unknown named module: '../images/feed.png'
After taking a look at this SO question, I figured it out:
const TabIcon = ({ selected, title, image }) => {
const selectColor = selected ? '#ED1B25' : '#FFF'
return (
<View style={[styles.tabStyle, {borderBottomColor: selectColor}]}>
<Image
style={{height: 35, width: 35}}
// Change HERE
source={image}
/>
</View>
)
}
Scenes
const Scenes = Actions.create(
<Scene key='root'>
<Scene key='tabbar' tabs={true} tabBarStyle={styles.tabBarStyle}>
//Change HERE
<Scene key='tab1' title='Feed' icon={TabIcon} image={require('../images/feed.png')}>

React Native Router Flux componentDidMount Fires in the Wrong Component

I have an alert to fire on a component's componentDitMount which is NOT an initial route (MainHome in this case). But when another component that is initial (MySchedule) is loaded it displays the alert. The routes are:
export default class Example extends React.Component {
render() {
return <Router createReducer={reducerCreate}>
<Scene key="modal" component={Modal} >
<Scene key="root" hideNavBar={true}>
<Scene key="tabbar" component={NavigationDrawer}>
<Scene key="main" tabs={true} default="tab2" >
<Scene key="my_schedule" component={MySchedule} hideTabBar={true} navBar={NavBar} initial={true} />
<Scene key="intro" component={Initial} hideTabBar={true} navBar={NavBar} />
<Scene key="home" component={Home} hideTabBar={true} navBar={NavBar} />
<Scene key="main_home" component={MainHome} hideTabBar={true} navBar={NavBar} />
</Scene>
</Scene>
</Scene>
<Scene key="error" component={Error}/>
</Scene>
</Router>;
}
}
And in the component MainHome's componentDidMount the alert is fired.
componentDidMount() {
Alert.alert(
'“Welcome...',
[
{text: 'OK'}
]
);
}
So in the initial component (MySchedule in this case) should not display the alert but it does and I can't figure out why. Your help would be much appreciated.
This is how NavigationView works.
<View
style={this.props.style}>
{this.props.navigationState.children.map(this._renderScene)}
</View>
NavigationExperimental is changing a lot, so this could change.
See https://github.com/facebook/react-native/issues/6579