redux 尝试从自定义上下文挂接访问状态变量时出错

aor9mmx1  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(129)

我已经用这个INITIAL_STATE定义了以下提供程序:

const WalletContext = createContext({} as any);

    const INITIAL_STATE = {
      isOpenWallets: false,
      pollWalletsCount: 0,
      wallets: [],
      walletSelected: undefined,
      walletFound: false,
      walletAPIConnected: false,
      walletAPI: undefined,
      walletAPIVersion: undefined,
      walletIsEnabled: false,
      walletDataLoaded: false,
      walletName: undefined,
      walletIcon: undefined,
      balance: undefined,
      networkId: undefined,
      utxos: undefined,
      tokens: undefined,
      collateralUtxos: undefined,
      changeAddress: undefined,
      addresses: [],
      publicKeys: undefined,
    };

export const WalletProvider = ({ children }: any) => {
  const {
    state,
    rehydrateLocalState,
    toggleWalletsHandler,
    handleWalletSelectHandler,
    connectWithWalletHandler,
    getWalletSelectedHandler,
    getBalanceHandler,
    getNetworkIdHandler,
    getUtxosHandler,
    getTokensHandler,
    getCollateralUtxosHandler,
    getChangeAddressHandler,
    getAddressesHandler,
    getPublicKeysHandler,
    getWalletDataHandler,
  } = useWalletsActions();

  const myStorage = { walletSelected: state.walletSelected, walletAPIConnected: state.walletAPIConnected }

  const { rehydrated, error } = useStorage(myStorage, rehydrateLocalState);

  return (
    <WalletContext.Provider
      value={{
        isOpenWallets: state.isOpenWallets,
        pollWalletsCount: state.pollWalletsCount,
        wallets: state.wallets,
        walletSelected: state.walletSelected,
        walletFound: state.walletFound,
        walletAPIConnected: state.walletAPIConnected,
        walletAPI: state.walletAPI,
        walletAPIVersion: state.walletAPIVersion,
        walletIsEnabled: state.walletIsEnabled,
        walletDataLoaded: state.walletDataLoaded,
        walletName: state.walletName,
        walletIcon: state.walletIcon,
        balance: state.balance,
        networkId: state.networkId,
        utxos: state.utxos,
        tokens: state.tokens,
        collateralUtxos: state.collateralUtxos,
        changeAddress: state.changeAddress,
        addresses: state.addresses,
        publicKeys: state.publicKeys,
        toggleWallets: toggleWalletsHandler,
        handleWalletSelect: handleWalletSelectHandler,
        connectWithWallet: connectWithWalletHandler,
        getWalletSelected: getWalletSelectedHandler,
        getBalance: getBalanceHandler,
        getNetworkId: getNetworkIdHandler,
        getUtxos: getUtxosHandler,
        getTokens: getTokensHandler,
        getCollateralUtxos: getCollateralUtxosHandler,
        getChangeAddress: getChangeAddressHandler,
        getAddresses: getAddressesHandler,
        getPublicKeys: getPublicKeysHandler,
        getWalletData: getWalletDataHandler,
      }}
    >
      {children}
    </WalletContext.Provider>
  );
};

export const useWallets = () => useContext(WalletContext);

我遇到的问题是,我得到了以下错误...

TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))

...当尝试访问处于该状态的变量之一时,使用我定义的自定义钩子,如下所示:

const [ walletFound ] = useWallets()

我做错了什么?
我已经用提供程序 Package 了所有组件,如下所示:

export default function App({ Component, pageProps }: AppProps) {
  return (
    <>
      <WalletProvider>
        <Header />
          <Component {...pageProps} />
        <Footer />
      </WalletProvider>
    </>
  )
}
dsekswqp

dsekswqp1#

我发现了错误。我在应该使用{}的时候使用了[],如下所示:

const { walletFound } = useWallets()

相关问题