首页 > 解决方案 > Navigation.navigate is not a function, navigation is undefined

问题描述

I have a problem, in React-Native.

<Button title="Save" onPress = {() => navigation.navigate('Save' , { image })}/>
    {image && <Image source = {{uri: image}} style= {{flex: 1}}/>}
    </View>
  );
}

I got an error:

navigation.navigate is not a function. (In 'navigation.navigate('Save', {image: image})', 'navigation.navigate' is undefined)

标签: javascriptreactjstypescriptreact-native

解决方案


Give your component a navigation prop like shown below.

Also make sure that it's added in Navigation stack.

function YourComponentName({navigation}){
return (
<View>
<Button title="Save" onPress = {() => navigation.navigate('Save' , { image })}/>
    {image && <Image source = {{uri: image}} style= {{flex: 1}}/>}
    </View>
  );
}

You can also use useNavigation hooks


推荐阅读