Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

Convert useContext + userReducer (JavaScript) to Typescript

$
0
0

I'm trying to convert two files from JS to Typescript, but with no luck.

The first file creates the reducer and actions to update the state.

The second one is a file to create the context and map the dispatch to actions.

The first problem is to pass the reducer and actions to the file "createDataContext.js", and the second is I can't find a proper type for boundActions and how to map inside the for.

Could someone please, give me a hand converting these files?

authContext.js

import createDataContext from "./createDataContext";import trackerApi from "../api/trackerApi";import AsyncStorage from "@react-native-community/async-storage";import { navigate } from "../navigationRef";const TOKEN_KEY = "@TrackersApp";const ACTIONS = {    ADD_ERROR: "add_error",    SIGN_UP: "sign_up",};const authReducer = (state, action) => {    switch (action.type) {        case ACTIONS.ADD_ERROR:            return { ...state, errorMessage: action.payload };        case ACTIONS.SIGN_UP:            return { token: action.payload, errorMessage: "" };        default:            return state;    }};const signUp = (dispatch) => async ({ email, password }) => {    try {        const response = await trackerApi.post("/signup", {            email,            password,        });        console.log(response);        await AsyncStorage.setItem(TOKEN_KEY, response.data.token);        dispatch({            type: ACTIONS.SIGN_UP,            payload: response.data.token,        });        navigate("TrackList");    } catch (error) {        dispatch({            type: ACTIONS.ADD_ERROR,            payload: "Something went wrong with Sign Up!",        });    }};const signIn = (dispatch) => {    return async ({ email, password }) => {        // go to api and signIn    };};const signOut = (dispatch) => {    return () => {        // manage async storage    };};export const { Provider, Context } = createDataContext(    authReducer,    { signUp, signIn, signOut },    {        token: null,        errorMessage: "",    });

createDataContext.js

import React, { createContext, useReducer } from "react";export default (reducer, actions, defaultValue) => {    const Context = createContext();    const Provider = ({ children }) => {        const [state, dispatch] = useReducer(reducer, defaultValue);        const boundActions = {};        for (let key in actions) {            boundActions[key] = actions[key](dispatch);        }        return (<Context.Provider                value={{                    state,                    ...boundActions,                }}>                {children}</Context.Provider>        );    };    return { Context, Provider };};

Thanks in advance!


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>