Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

reactjs - How to set the passing parameters to the button using Navigation 5 of react native

I have problem on navigating the other screen of my project. Right now I have a task where when I click the button I will go the group chat screen. this screen must pass the parameters to the routes. So the error that I encounter today is undefined is not an object(evaluating 'props.navigation')

Goal : When I click the button my screen will navigate the GroupChat

Error: Undefined Is not an object(evaluating props.navigation')

Adding More Details

What I have right now is.

  1. Driver Dashboard Screen.
  2. Fetch Request Screen
  3. Group Chat Screen

On my Driver Dashboard:

      async Watcher() {
    try {

      this.watchId = geolocation.getCurrentPosition(
        (position) => {
          this.setState({
            region: {
              latitude: position.coords.latitude,
              longitude: position.coords.longitude,
              latitudeDelta: 0.009,
              longitudeDelta: 0.009,
            },
            error: null,
          });


        },
        (error) => this.setState({ error: error.message }),
        { enableHighAccuracy: false, timeout: 50000 },
      );

    } catch (error) {
      console.log(error);
    }
  }

  async componentDidMount() {

    this.Watcher();

  }

    <FetchRequest navigation={this.props.navigation}/>

On My Fetch Screen:

    const FetchRequest = ({navigation,props}) => {
    
    return (
        <View style={{ justifyContent: 'flex-end', flex: 1, padding: 20 }}>
            <Card
            style={{ justifyContent: 'center', borderRadius: 20, elevation: 4 }}>
            <CardItem>
                <Left>
                <Body>
                    <View style={{ flexDirection: 'row', padding: 20 }}>
                        <View
                            style={{
                            flexDirection: 'column',
                            flex: 1,
                            justifyContent: 'space-between',
                            }}>
                            <View
                            style={{
                                position: 'absolute',
                                left: -25,
                                top: '40%',
                            }}>
                            <DotTrail/>   
                            </View>
                            <Text
                            style={{
                                alignSelf: 'center',
                                color: '#000',
                                fontWeight: 'bold',
                                fontSize: 17,
                            }}
                            adjustsFontSizeToFit
                            numberOfLines={2}>
                            Pick Up Request {'
'}
                            </Text>
                            <Item></Item>
                            <Text></Text>
                            <Text style={{ fontSize: 14, fontWeight:'bold' }}>
                                Location{' '}
                            </Text>
                            
                            <Text></Text>

                            <Item>
                                <Text style={{ fontSize: 14, fontWeight:'bold' }}>
                                    Location{'
'}
                                </Text>
                            </Item>
                        </View>
                    </View>

                    <View style={{ flexDirection: 'row' }}>
                    <View
                        style={{
                        flexDirection: 'row',
                        flex: 1,
                        justifyContent: 'space-evenly',
                        }}>
                        <Button onPress={() => {
                        /* 1. Navigate to the Details route with params */
                        props.navigation.navigate('GroupChat', {
                            itemId: 86,
                            otherParam: 'anything you want here',
                        });
                        }} small primary title="Message">
                            <Text>Message</Text>
                        </Button>
                        <Button small warning title="Fetch">
                            <Text>Fetch</Text>
                        </Button>
                        <Button small light title="Cancel">
                            <Text>Cancel</Text>
                        </Button>
                    </View>
                    </View>
                </Body>
                </Left>
            </CardItem>
            </Card>
        </View>
    )

}




export default FetchRequest;

On my Group Chat Screen:

    class GroupChat extends React.Component {



    render() {
        
        return (
            <SafeAreaView style={[{ backgroundColor: '#ffff' }]}>
                <Text>Group Chat Goes...</Text>
            </SafeAreaView>
        )
    }
}

export default GroupChat;

Here is the output inside of my Driver Dashboard:

Current Output

Illustration Error:

Error Illustration

Hope someone can produce explanation why I encounter this error and how to solve this. Thank you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Simply use navigation.navigate()

<Button onPress={() => {
                        /* 1. Navigate to the Details route with params */
                        navigation.navigate('GroupChat', {
                            itemId: 86,
                            otherParam: 'anything you want here',
                        });
                        }} 

also, remove props from FetchRequest:

const FetchRequest = ({navigation}) => {
//.......

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...