nmccready/use-global-hook

error on type of actions

Closed this issue · 1 comments

Property 'addToCounter' does not exist on type 'OuterBaseActions<{ counter: number; }>'.ts(2339)

import React from 'react';
import useGlobalHook from '@znemz/use-global-hook';
export declare type SetStateFn<T> = (newState: T, isRef?: boolean, doDigest?: boolean) => void;
const initialState = { counter: 0, };

type State = { state: typeof initialState, setState: SetStateFn<Partial<typeof initialState>> };

const actions = {
    addToCounter(store: State, amount: number) {
        const newCounterValue = store.state.counter + amount;
        store.setState({ counter: newCounterValue });
    },
};

const useGlobal = useGlobalHook({ React, initialState, actions });

const App = () => {
    const [globalState, globalActions] = useGlobal();
    return (
        <div>
            <p> counter: {globalState.counter} </p>
            < button type="button" onClick={() => globalActions.addToCounter(1)}>
                +1 to global
            </button>
        </div>
    );
};

This is because you need to explicitly pass in your <StateType, InnerActions, OutterActions> typescript cannot always just infer everything.

Example:

useGlobalHook<Api, InnerActions, OuterActions>(...)