减速机错误。初始状态。我如何初始化状态并使用 action.type

Error In Reducer . initialState . How i can initialize state and use action.type

我正在尝试设置登录用户。使用减速器。 错误是:

意外的令牌 && 语法错误

线路切换错误(action.type) 或括号中的错误未正确关闭我不知道
它将在 reducer initialize 和 initialState

中使用 : 或 =

Whosebug:看起来您的 post 主要是代码;请添加更多详细信息。

我:我已经分享了足够的细节我不知道的比上面更多

 import React from 'react';
    import {View, StyleSheet, Text} from 'react-native';
    import {NavigationContainer} from '@react-navigation/native';
    import {createStackNavigator} from '@react-navigation/stack';
    import { AuthStackNavigator } from './navigators/AuthStackNavigator';
    import { AuthContext } from './context/AuthContext';
    import  axios  from "axios";
    import { BASE_URL } from './config';
    import { sleep } from './utils/sleep';
    import { createAction } from './utils/createAction';
    const RootStack = createStackNavigator();
    
    export default function ()
    {
      
      const [ state, dispatch ] = React.useReducer(
        reducer : ( state: { user: * }, action ) =>
        {
          switch ( action.type )
          {
            case 'SET_USER':
              return {
                ...state,
                user: { ...action.payload },
              };
            default:
              return state;
          }
        },
        initialState : {
          user: undefined,
        }
      );
    
      const auth = React.useMemo( () => ( {
    
        login: async (email,password) =>
        {
              
    const options = {
      method: 'post',
      url: `${BASE_URL}/auth/local`,
      data: {
        identifier: email,
        email,
        password,
      },
      transformResponse: [
        (data) => {
          // transform the response
          return data;
        },
      ],
    };
    await sleep(2000);
    
    // send the request
    const {data}= axios( options );
    
          const user = {
            email: data.user.email,
            token: data.jwt,
          };
       dispatch(createAction(type:'SET_USER',user))
        },
    
    
        logout: () =>
        {
          console.log( 'logout' );
        },
    
    
        register:async (email,password) =>
        {
    const options = {
      method: 'post',
      url: `${BASE_URL}/auth/local/register`,
      data: {
        user: email,
        email,
        password,
      },
      transformResponse: [
        (data) => {
          // transform the response
    
          return data;
        },
      ],
    };
    await sleep(2000);
    
    // send the request
        
    
    
          // console.log( 'register' );
          // await axios.post(url:`${BASE_URL}/auth/local/register`, data:{
          //   username: email,
          //   email,
          //   password,
          // } );
        },
      } ),
      );
    
    
    console.log(state.user)
    
      return (
        <AuthContext.Provider value={auth}>
          <NavigationContainer>
            <RootStack.Navigator screenOptions={ { headerShown: false } }>
              <RootStack.Screen name={ 'AuthStack' } component={ AuthStackNavigator } />
            </RootStack.Navigator>
          </NavigationContainer>
        </AuthContext.Provider>
      );
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center',
      },
    });

如果我理解你的错误并正确地发出它似乎与你试图定义 useReducer 挂钩参数的方式有关。您似乎试图定义对象键,但您没有定义对象。 useReducer 钩子函数只使用最多 3 个参数。

useReducer

const [state, dispatch] = useReducer(reducer, initialArg, init);

通常你只需要前两个,即你的reducer函数和初始reducer状态值。您可以在文档中进一步阅读有关延迟初始化的第三个参数的信息。

const [state, dispatch] = React.useReducer(
  (state, action ) => { // <-- reducer function
    switch (action.type) {
      case 'SET_USER':
        return {
          ...state,
          user: { ...action.payload },
        };
      default:
        return state;
    }
  },
  { user: undefined }, // <-- initial state object value
);