use*_*224 13 javascript reactjs react-native expo
我遵循本教程https://reactnavigation.org/docs/intro/ ,我遇到了一些问题.
我使用Expo Client应用程序每次渲染我的应用程序而不是模拟器/模拟器.
我的代码在下面看到.
我最初在"ChatScreen"组件上面定义了"SimpleApp"const但是这给了我以下错误:
路线'聊天'应该声明一个屏幕.例如:......等
所以我将SimpleApp的decleration移到了"AppRegistry"之上,并标记了一个新的错误
元素类型无效:期望字符串.....您可能忘记导出组件..等等
教程没有将关键词"export default"添加到任何组件中,我认为它可能与我在Expo应用程序上运行它的事实有关吗?所以我将"export default"添加到"HomeScreen",错误就消失了.
我似乎无法摆脱的新错误(基于下面的代码)如下:
undefined不是对象(评估'this.props.navigation.navigate')
我无法摆脱它,除非我删除"const {navigate}"周围的"{}"但是当我从主屏幕按下按钮时会破坏导航
import React from 'react';
import {AppRegistry,Text,Button} from 'react-native';
import { StackNavigator } from 'react-navigation';
export default class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
};
render() {
const { navigate } = this.props.navigation;
return (
<View>
<Text>Hello, Chat App!</Text>
<Button
onPress={() => navigate('Chat')}
title="Chat with Lucy"
/>
</View>
);
}
}
class ChatScreen extends React.Component {
static navigationOptions = {
title: 'Chat with Lucy',
};
render() {
return (
<View>
<Text>Chat with Lucy</Text>
</View>
);
}
}
const SimpleApp = StackNavigator({
Home: { screen: HomeScreen },
Chat: { screen: ChatScreen },
});
AppRegistry.registerComponent('SimpleApp', () => SimpleApp);
Run Code Online (Sandbox Code Playgroud)
小智 31
附加信息:当您嵌套子组件时,您需要在父组件中将导航作为 prop 传递。//父.js
<childcomponent navigation={this.props.navigation}/>
你可以像这样访问导航
//child.js
this.props.navigation.navigate('yourcomponent');
参考:https : //reactnavigation.org/docs/en/connecting-navigation-prop.html
小智 20
有了世博会,你不应该让你的自己申请注册,你应该让世博会这样做,记住你必须始终导出默认组件:你还需要从反应本地导入视图和按钮:请在下面找到完整的码:
import React from 'react';
import {
AppRegistry,
Text,
View,
Button
} from 'react-native';
import { StackNavigator } from 'react-navigation';
class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
};
render() {
const { navigate } = this.props.navigation;
return (
<View>
<Text>Hello, Chat App!</Text>
<Button
onPress={() => navigate('Chat', { user: 'Lucy' })}
title="Chat with Lucy"
/>
</View>
);
}
}
class ChatScreen extends React.Component {
// Nav options can be defined as a function of the screen's props:
static navigationOptions = ({ navigation }) => ({
title: `Chat with ${navigation.state.params.user}`,
});
render() {
// The screen's current route is passed in to `props.navigation.state`:
const { params } = this.props.navigation.state;
return (
<View>
<Text>Chat with {params.user}</Text>
</View>
);
}
}
const SimpleAppNavigator = StackNavigator({
Home: { screen: HomeScreen },
Chat: { screen: ChatScreen }
});
const AppNavigation = () => (
<SimpleAppNavigator />
);
export default class App extends React.Component {
render() {
return (
<AppNavigation/>
);
}
}
Run Code Online (Sandbox Code Playgroud)
正如 Bobur 在他的回答中所说,导航道具不会传递给路由组件的子组件。要让您的组件能够访问,navigation您可以将其作为 prop 传递给它们,但还有更好的方法。
如果您不想将navigationprop 一直传递到组件层次结构中,useNavigation则可以使用。(在我看来,这无论如何都是更干净的,并且减少了我们必须编写的代码量):
function MyBackButton() {
const navigation = useNavigation();
return (
<Button
title="Back"
onPress={() => {
navigation.goBack();
}}
/>
);
}
Run Code Online (Sandbox Code Playgroud)
https://reactnavigation.org/docs/use-navigation/
这真的很好,因为如果您有多个级别的组件,您将不必连续传递导航对象作为 props 只是为了使用它。仅传递navigation一次需要我们 1. 将 prop 添加到我们想要将其传递到的组件。2.从父组件传递prop。3.使用navigation道具进行导航。有时我们必须重复步骤 1 和 2,将 prop 一直传递到需要使用的组件navigation。useNavigation我们可以使用此方法将步骤 1 和 2 压缩为单个调用,无论它们重复多少次。
我认为这是最好的。
| 归档时间: |
|
| 查看次数: |
24964 次 |
| 最近记录: |