AuthContext 未定义反应原生

时间:2021-06-18 14:24:19

标签: react-native

大家好,我被困在一个关于上下文的解决方案上,我做了在文档上下文中解释的每一件事,你能帮我吗 我的上下文组件


const AuthContext = React.createContext();

export const UseAuth = () => {
  return useContext(AuthContext);
};

const login = () => {
  try {
    const response = fetch(
      'my api',
      {
        method: 'POST',
        headers: new Headers({
          'Content-Type': `application/json`,
        }),
        body: JSON.stringify({email: userEmail, password: userPassword}),
      },
    );

    const json = response.json();
    const token = json.data.access_token;
    console.log(token);
  } catch (error) {
    console.log(error);
  }
};
export const AuthProvider = ({children}) => {
  const value = {
    login,
  };
  return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
};

还有我的 App.js

import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
import HrProfile from './src/components/HrProfile';
import HiringEventDetails from './src/pages/HiringEventDetails';
import HiringEventDate from './src/pages/HiringEventDate';
import HiringEventPlace from './src/pages/HiringEventPlace';
import HiringEventDuration from './src/pages/HiringEventDuration';
import HiringEventTime from './src/pages/HiringEventTime';
import SuccessPageAnimation from './src/pages/SuccessPageAnimation';
import SignIn from './src/pages/SignIn';
import AuthProvider from './src/components/Context';
// const onRemove = id => e => {
//   setRemove(remove.filter(remove => remove.id !== id));
// };
const Stack = createStackNavigator();

const App = () => {
  const [item, setItem] = useState({items: []});
  const [login, setLogin] = useContext(AuthProvider);
  return (
    <AuthProvider>
      <NavigationContainer>
        <Stack.Navigator
          item={item}
          setItem={setItem}
          initialRouteName="SignIn"
          screenOptions={{
            headerShown: false,
            cardStyle: {backgroundColor: '#fff'},
          }}>
          <Stack.Screen name="SignIn" component={SignIn} />
          <Stack.Screen name="HrProfile" component={HrProfile} />
          <Stack.Screen
            name="HiringEventDetails"
            component={HiringEventDetails}
          />
          <Stack.Screen name="HiringEventPlace" component={HiringEventPlace} />
          <Stack.Screen
            name="HiringEventDuration"
            component={HiringEventDuration}
          />
          <Stack.Screen name="HiringEventDate" component={HiringEventDate} />
          <Stack.Screen name="HiringEventTime" component={HiringEventTime} />
          <Stack.Screen
            name="SuccessPageAnimation"
            component={SuccessPageAnimation}
          />
        </Stack.Navigator>
      </NavigationContainer>
    </AuthProvider>
  );
};

export default App;

响应我未定义不是对象(评估'Context._context') 所以我被困了 2 天,很难理解,因为我是 React Native 的初学者

0 个答案:

没有答案