gpt4 book ai didi

javascript - 在 React Native 中将应用程序导航结构从版本 4 更改为版本 5

转载 作者:行者123 更新时间:2023-12-04 23:55:17 25 4
gpt4 key购买 nike

我正在使用 React Navigation 版本 4 开发一个旧应用程序,该应用程序显然包含一个注册和登录页面,然后是应用程序的内容。

最近我开始使用 React Navigation 版本 5 重新制作应用程序的内容,以便使用共享元素动画和底部选项卡导航器,这非常简单。

但我很难将登录部分转换为版本 5,因为应用程序结构有些复杂,而且我对 React 导航版本 5 还有些陌生。

我将在下面留下应用程序结构图和所用代码示例。

Navigation used in the app

App.js :

import { setNavigator } from "./app/navigationRef";
const articleListFlow = createStackNavigator({
Main: MainScreen, // screen with diffrent articles categories
ResultsShow: ResultShowScreen, // article details screen
});
const loginFlow = createStackNavigator({
Signup: SignupScreen,
Signin: SigninScreen,
});
loginFlow.navigationOptions = () => {
return {
headerShown: false,
};
};

articleListFlow.navigationOptions = {
title: "News Feed",
tabBarIcon: ({ tintColor }) => (
<View>
<Icon style={[{ color: tintColor }]} size={25} name={"ios-cart"} />
</View>
),
activeColor: "#ffffff",
inactiveColor: "#ebaabd",
barStyle: { backgroundColor: "#d13560" },
};
const switchNavigator = createSwitchNavigator({
ResolveAuth: ResolveAuthScreen,
MainloginFlow: createSwitchNavigator({
//WelcomeScreen: WeclomeScreen,
loginFlow: loginFlow,
}),

mainFlow: createMaterialBottomTabNavigator(
{
articleListFlow: articleListFlow,
ArticleSave: ArticleSaveScreen, // we dont need this one
Account: AccountScreen,
},
{
activeColor: "#ffffff",
inactiveColor: "#bda1f7",
barStyle: { backgroundColor: "#6948f4" },
}
),
});
const App = createAppContainer(switchNavigator);
export default () => {
return (
<AuthProvider>
<App
ref={(navigator) => {
setNavigator(navigator);
}}
/>
</AuthProvider>
);
};

NavigationRef.js :

import { NavigationActions } from "react-navigation";

let navigator;

export const setNavigator = (nav) => {
navigator = nav;
};

export const navigate = (routeName, params) => {
navigator.dispatch(
NavigationActions.navigate({
routeName,
params,
})
);
};

// routename is the name of the routes singin singup accountscreen
// params information we want to pass to the screen we want to show

AuthContext.js

import { AsyncStorage } from "react-native";
import createDataContext from "./createDataContext";
import userAPI from "../api/user";

// using navigate to access the navigator and redirect the user
import { navigate } from "../navigationRef";

// AUTHENTICATION REDUCERS
const authReducer = (state, action) => {
switch (action.type) {
case "add_error": {
return {
...state,
errorMessage: action.payload,
};
}

case "clear_error_message": {
return {
...state,
errorMessage: "",
};
}
case "signin": {
return {
errorMessage: "",
token: action.payload,
};
}

default:
return state;
}
};

// CLEARING ERROR MESSAGES WHEN SWITCHING SIGNIN-SIGNUP
const clearErrorMessage = (dispatch) => () => {
dispatch({ type: "clear_error_message" });
};

// AUTOMATIC SIGNIN ONLY USING TOKENS ON USER DEVICE
const tryLocalSignin = (dispatch) => async () => {
const token = await AsyncStorage.getItem("token");
if (token) {
// if token exists
dispatch({ type: "signin", payload: token });

navigate("Main");
} else {
// if token doesnt exist
navigate("WelcomeScreen");
}
};

// SIGNUP
const signup = (dispatch) => async ({ email, password }) => {
try {
const response = await userAPI.post("/signup", { email, password });
await AsyncStorage.setItem("token", response.data.token);
dispatch({ type: "signin", payload: response.data.token });

// making use of the navigate component to access navigation
// and redirect the user
navigate("Main");
} catch (err) {
dispatch({
type: "add_error",
payload: "Something went wrong with sign up",
});
}
};

// SIGNIN
const signin = (dispatch) => async ({ email, password }) => {
try {
const response = await userAPI.post("/signin", { email, password });
await AsyncStorage.setItem("token", response.data.token);
// using signin since the logic is the same
dispatch({ type: "signin", payload: response.data.token });

// making use of the navigate component to access navigation
// and redirect the user
navigate("Main");
} catch (err) {
console.log(err);
dispatch({
type: "add_error",
payload: "Something went wrong with sign in",
});
}
};

// SIGNOUT
const signout = (dispatch) => async () => {
// removing the token makes identification not work again
await AsyncStorage.removeItem("token");
dispatch({ type: "signout" });

navigate("loginFlow");
};

// CREATING CONTEXT AND PROVIDER OBJECTS FOR AUTHENTICATION
export const { Provider, Context } = createDataContext(
authReducer,
{
signin,
signup,
signout,
clearErrorMessage,
tryLocalSignin,
},
{
token: null,
errorMessage: "",
}
);

createDataContext.js

import React, { useReducer } from "react";

export default (reducer, actions, defaultValue) => {
const Context = React.createContext();

const Provider = ({ children }) => {
const [state, dispatch] = useReducer(reducer, defaultValue);

const boundActions = {};

for (let action in actions) {
// for every action in the actions, call it with dispatch
boundActions[action] = actions[action](dispatch);
}

return (
<Context.Provider value={{ state, ...boundActions }}>
{children}
</Context.Provider>
);
};

return { Context, Provider };
};

对于长代码,我深表歉意,并提前感谢任何可以提供帮助的人。

最佳答案

从 V4 迁移到 V5 时,您需要考虑几件事情,它涉及一些更改,您还可以考虑使用 Hook 等功能。

第一个更改是删除 Switch Navigator 并有条件地在其位置呈现导航器。这将在您的 App.js 中完成。由于您已经有一个基于 reducer 的实现,您可以使用状态值来做出这个决定。

下一个变化将是堆栈的创建,在 V4 中,您通过传递屏幕来创建导航,现在一切都是一个组件,您将屏幕作为子项传递。

选项也作为 Prop 发送给导航器或屏幕本身。

navigation ref的用法仍然可行,但您也可以在组件内部使用诸如 usenavigation 之类的 Hook ,并且对于您的身份验证流程,您不会在有条件地呈现导航器时使用它。

我根据你的代码做了一个简化版。应用程序.js

const AuthStack = createStackNavigator();
const AppTabs = createMaterialBottomTabNavigator();
const ArticleStack = createStackNavigator();

const Articles = () => {
return (
<ArticleStack.Navigator>
<AppTabs.Screen name="ArticlesList" component={ArticleList} />
<AppTabs.Screen name="ArticlesDetails" component={ArticleDetail} />
</ArticleStack.Navigator>
);
};

export default function App() {
const [state, dispatch] = React.useReducer(authReducer, {
isLoading: true,
token: null,
errorMessage: '',
});

React.useEffect(() => {
const bootstrapAsync = async () => {
const userToken = await AsyncStorage.getItem('userToken');
dispatch({ type: 'RESTORE_TOKEN', token: userToken });
};

bootstrapAsync();
}, []);
const authContext = React.useMemo(
() => ({
signIn: async (data) => {
dispatch({ type: 'SIGN_IN', token: 'dummy-auth-token' });
},
signOut: () => dispatch({ type: 'SIGN_OUT' }),
signUp: async (data) => {
dispatch({ type: 'SIGN_IN', token: 'dummy-auth-token' });
},
}),
[]
);

return (
<AuthContext.Provider value={authContext}>
<NavigationContainer>
{state.token === null ? (
<AuthStack.Navigator headerMode="none">
{state.isLoading ? (
<AuthStack.Screen name="Welcome" component={WelcomeScreen} />
) : (
<>
<AuthStack.Screen name="SignIn" component={SignInScreen} />
<AuthStack.Screen name="SignUp" component={SingUpScreen} />
</>
)}
</AuthStack.Navigator>
) : (
<AppTabs.Navigator
activeColor="#f0edf6"
inactiveColor="#3e2465"
barStyle={{ backgroundColor: '#694fad' }}>
<AppTabs.Screen
name="Articles"
component={Articles}
options={{
tabBarLabel: 'Home',
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons
name="home"
color={color}
size={size}
/>
),
}}
/>
<AppTabs.Screen name="Search" component={SearchScreen} />
<AppTabs.Screen name="Save" component={SaveScreen} />
<AppTabs.Screen name="Account" component={AccountScreen} />
</AppTabs.Navigator>
)}
</NavigationContainer>
</AuthContext.Provider>
);
}

授权上下文

const AuthContext = React.createContext();
export default AuthContext;

授权 reducer

export const authReducer = (state, action) => {
switch (action.type) {
case 'RESTORE_TOKEN':
return {
...state,
token: action.token,
isLoading: false,
};

case 'SIGN_IN': {
return {
errorMessage: '',
token: action.payload,
};
}

case 'SIGN_OUT': {
return {
errorMessage: '',
token: null,
};
}

default:
return state;
}
};

如您所见,流程将显示欢迎屏幕,直到从异步存储加载 token ,然后基于此显示选项卡或登录屏幕。参数也作为 Prop 传递。我已将操作移至 app.js,但它也可以分开。

您可以在此处查看完整运行的示例 https://snack.expo.io/@guruparan/navigation-sample-3

希望对您有所帮助,如有任何问题,欢迎随时提问。

关于javascript - 在 React Native 中将应用程序导航结构从版本 4 更改为版本 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62092778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com