Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SetParams handling in TabRouter.js #132

Merged
merged 4 commits into from
Feb 8, 2017
Merged

Add SetParams handling in TabRouter.js #132

merged 4 commits into from
Feb 8, 2017

Conversation

microwavesafe
Copy link

@microwavesafe microwavesafe commented Feb 1, 2017

Following on from #126 setParams in TabNavigator was not working. I believe this is because it is not handled in the getStateForAction function in TabRouter.js

The following code works with my changes

import React from 'react';
import {
    AppRegistry,
    Text,
    View,
    Button,
    StyleSheet
} from 'react-native';
import { TabNavigator, TabView } from 'react-navigation';
import Ionicons from 'react-native-vector-icons/Ionicons';
import IconBadge from './IconBadge'

class Home extends React.Component {

    static navigationOptions = {
        tabBar: ({ state }) => ({
            label: 'Home',
            icon: ({ tintColor, focused }) =>
                <IconBadge
                MainElement={
                    <Ionicons
                    name={focused ? 'ios-home' : 'ios-home-outline'}
                    size={26}
                    style={{ color: tintColor }}
                    />
                }
                badgeNumber={typeof state.params === 'undefined' ? 0 : state.params.badgeCount}
                />,
        }),
    }

    render() {
        const {navigation} = this.props;

        return (
            <View>
                <Button
                    onPress={() => navigation.setParams({badgeCount : 1})}
                    title="Set Badge Number"
                />
            </View>
        );
    }
}

class Settings extends React.Component {
    static navigationOptions = {
        tabBar: {
            label: 'Settings'
        },
    }

    render() {
        const { navigate } = this.props.navigation;

        return (
            <Button
                onPress={() => this.props.navigation.goBack()}
                title="Go back"
            />
        );
    }
}

const MyApp = TabNavigator({
    Home: {
        screen: Home,
    },
    Settings: {
        screen: Settings,
    },
}, {
    ...TabNavigator.Presets.iOSBottomTabs,
    tabBarOptions: {
        activeTintColor: '#e91e63',
    },
});

AppRegistry.registerComponent('ReactNavigation', () => MyApp);

And fails without them. I'm presuming this is how you intended it to work, so please dismiss if I have misunderstood.

This was referenced Feb 2, 2017
@agrcrobles
Copy link

I noticed what you are saying...
When doing props.navigate("Page", { index: 1 }) in a TabRouter then I can't access navigation.state.params to get the index.

@microwavesafe
Copy link
Author

Actually that is done through merging the params see #134. This issue only resolves calling

this.props.navigation.setParams(...)

We need to get both bugs fixed to get params working properly in TabNavigator

@ericvicenti
Copy link
Contributor

This looks great. Can you add a test for this, or maybe share the SetParams test for StackRouter?

@microwavesafe
Copy link
Author

OK I'm not going to lie, I have no idea how the tests work (I really work in embedded programming, where tests are dependent on hardware!). So I've just cut and paste from the StackRouter test and changed it to TabRouter.

@ericvicenti
Copy link
Contributor

To run the tests, it should be as easy as npm install and npm test. Does the new test pass after your changes are added, and fail before?

@microwavesafe
Copy link
Author

I have managed to get the test working.

@microwavesafe
Copy link
Author

microwavesafe commented Feb 8, 2017

@ericvicenti @satya164

This code doesn't work with current head. It requires

if (action.type === NavigationActions.SET_PARAMS)

not

if (action.type === 'SetParams')

shall I close this and open a new pull request based on the current head?

@satya164
Copy link
Member

satya164 commented Feb 8, 2017

You can rebase the branch against master or merge master branch to this branch - git rebase master or git merge master

@microwavesafe
Copy link
Author

OK this now works with latest master branch, including tests. It would be good if we could get it merged.

@satya164 satya164 merged commit c4787a4 into react-navigation:master Feb 8, 2017
sourcecode911 pushed a commit to sourcecode911/react-navigation that referenced this pull request Mar 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants