Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React navigation state.params not working

It is not working. I tried everything. Nothing works. state.params is simply not there if you make an advanced app.

I have this problem with the react "navigation". It says in the manual that the params object should be there https://reactnavigation.org/docs/navigation-prop.html#state-the-screen-s-current-state-route But it isn't.

I set an id parameter like this in screen 1 when I link to screen 2:

 <TouchableWithoutFeedback onPress={ ()=> this.props.navigation.navigate('FontsTab', { id: item.id }) } style={styles.listHeader} >
                <View  style={styles.listRowContainer}>
                    <View  style={styles.listinside1Container}>
                        <Image  style={styles.listImage} source={item.icon} />
                        <View  style={styles.listContainer} onPress={(event) => this._selectedItem(item.text)}  >
                            <Text style={styles.listHeader} >
                              {item.title}
                            </Text>
                            <Text  style={styles.listValue} >{item.value}</Text>
                            <Image 
                                style={{width: 50, height: 50}}
                                source={{uri: item.img}}
                            />

                        </View>
                    </View>

                                </View>
            </TouchableWithoutFeedback>

But it's not working. In screen 2 I can't use the state.params :

     <ScrollView style={styles.container}>
          <Text>{ JSON.stringify(this.props.navigation)}</Text>
          <Text>TEST{ state.params }</Text>
          <Image
              style={{width: 150, height: 150}}
              source={{uri: this.state.dataSource.img}}
          />
          <Text  style={styles.textStyle} >{this.state.dataSource.text}</Text>
      </ScrollView>

state.params just returns nothing. What can I do about it?

The full class for screen2:

class Fonts extends Component {
    constructor(props) {
        super(props);
        this.state = {
            params: null,
            selectedIndex: 0,
            value: 0.5,
            dataSource: null,
            isLoading: true
        };
        this.componentDidMount = this.componentDidMount.bind(this);
    }
    getNavigationParams() {
        return this.props.navigation.state.params || {}
    }

    componentDidMount(){
        return fetch('http://www.koolbusiness.com/newvi/4580715507220480.json')
            .then((response) => response.json())
            .then((responseJson) => {
                this.setState({
                    ...this.state,
                    isLoading: false,
                    dataSource: responseJson,
                }, function(){
                });
            })
            .catch((error) =>{
                console.error(error);
            });
    }
  render() {
      if(this.state.isLoading){
          return(
              <View style={{flex: 1, padding: 20}}>
                  <ActivityIndicator/>
              </View>
          )
      }
    return (
      <ScrollView style={styles.container}>
          <Text>{ JSON.stringify(this.props)}</Text>
          <Text>TEST{ this.state.params }</Text>
          <Image
              style={{width: 150, height: 150}}
              source={{uri: this.state.dataSource.img}}
          />
          <Text  style={styles.textStyle} >{this.state.dataSource.text}</Text>
      </ScrollView>
    );
  }
}

In my app this pain is reproducible by a simple button in screen1:

<Button onPress={() => navigate('FontsTab', { name: 'Brent' })} title="Go to Brent's profile" />

Then switching to the FontsTab works but the params are not in the state object:

enter image description here

I also have this code for the tabview

import React, { Component } from 'react';
import { View, Text, StyleSheet } from 'react-native';

import { StackNavigator } from 'react-navigation';
import { Icon } from 'react-native-elements';

import FontsHome from '../views/fonts_home';
import FontsDetails from '../views/fonts_detail';

const FontsTabView = ({ navigation }) => (
  <FontsHome banner="Fonts" navigation={navigation} />
);

const FontsDetailTabView = ({ navigation }) => (
  <FontsDetails banner="Fonts Detail" navigation={navigation} />
);

const FontsTab = StackNavigator({
  Home: {
    screen: FontsTabView,
    path: '/',
    navigationOptions: ({ navigation }) => ({
      title: '',
      headerLeft: (
        <Icon
          name="menu"
          size={30}
          type="entypo"
          style={{ paddingLeft: 10 }}
          onPress={() => navigation.navigate('DrawerOpen')}
        />
      ),
    }),
  },
  Detail: {
    screen: FontsDetailTabView,
    path: 'fonts_detail',
    navigationOptions: {
      title: 'Fonts Detail',
    },
  },
});

export default FontsTab;
like image 917
Niklas Rosencrantz Avatar asked Nov 08 '22 09:11

Niklas Rosencrantz


1 Answers

this.props.navigation.state.params.id will give you the value of param id passed from screen1.

like image 132
Harikrishnan Avatar answered Nov 15 '22 13:11

Harikrishnan