请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

TypeError: undefined is not an object (evaluating 'routes[index].params.theme')

正在回答 回答被采纳积分+3

插入代码

2回答

CrazyCodeBoy 2018-07-09 09:57:49


确认下react-navigation的版本如果是2.0以上版本参考如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import {createDrawerNavigator, createStackNavigator,SafeAreaView,DrawerItems} from 'react-navigation';
import {createBottomTabNavigator, BottomTabBar} from 'react-navigation-tabs';
 
class TabBarComponent extends React.Component {
    constructor(props) {
        super(props);
        this.theme = {
            tintColor: props.activeTintColor,
            updateTime: new Date().getTime()
        }
    }
 
    render() {
        const {routes, index} = this.props.navigation.state;
        const {theme} = routes[index].params;
        if (theme && theme.updateTime > this.theme.updateTime) {
            this.theme = theme;
        }
        /**
         * custom tabBarComponent
         * https://github.com/react-navigation/react-navigation/issues/4297
         */
        return (
            <BottomTabBar
                {...this.props}
                activeTintColor={this.theme.tintColor || this.props.activeTintColor}
            />
        );
    }
 
}
 
export const AppTabNavigator = createBottomTabNavigator({
    Page1: {
    ...


0 回复 有任何疑惑可以回复我~
r4ggjlx 2018-07-07 23:59:03

routes[index].params ===undefined?

0 回复 有任何疑惑可以回复我~
  • 提问者 liuxiaozhu #1
    想知道根本原因,routes[index] 跟 theme 到底是哪里来的?课程里根本没有提到。
    回复 有任何疑惑可以回复我~ 2018-07-08 09:06:41
  • CrazyCodeBoy 回复 提问者 liuxiaozhu #2
    theme 在Page1中有介绍:
    
    <Button
                    title="改变主题色"
                    onPress={() => {
                        navigation.setParams({theme:{
                            tintColor:'orange',
                            updateTime:new Date().getTime()
                        }})
                    }}
                />
    回复 有任何疑惑可以回复我~ 2018-07-09 09:57:29
  • 提问者 liuxiaozhu 回复 CrazyCodeBoy #3
    第一次加载的时候,确实是 undefined。我想知道第一次加载时,在哪里配置传入这个 params ?
    回复 有任何疑惑可以回复我~ 2018-07-11 06:48:37
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号