如何在React TypeScript中定义允许为null的上下文类型?

huangapple go评论60阅读模式
英文:

How to define type to allow null in createContext React TypeScript?

问题

I have issue about typescript when creating modal context with typescript, I want to null as default value in createContext but it will causing typescript error

import React from 'react';

type TModal = boolean;
type TModalContext = [TModal, React.Dispatch<React.SetStateAction<TModal>>];

const ModalContext = React.createContext<TModalContext | null>(null); // allow null as default value

const ModalProvider = ({ children }: { children: React.ReactNode }) => {
    const modalState = useState<TModal>(false);

    return <ModalContext.Provider value={modalState}>{children}</ModalContext.Provider>;
};

const useModalContext = () => React.useContext(ModalContext);

When I call useModalContext hook to use in another component I get an error:

Type 'TModalContext | null' is not an array type.ts(2461)

How can I resolve this issue ?

英文:

I have issue about typescript when creating modal context with typescript, I want to null as default value in createContext but it will causing typescript error

import React from &#39;react&#39;;

type TModal = boolean;
type TModalContext = [TModal, React.Dispatch&lt;React.SetStateAction&lt;TModal&gt;&gt;];

const ModalContext = React.createContext&lt;TModalContext | null&gt;(null); // allow null as defaut value

const ModalProvider = ({ children }: { children: React.ReactNode }) =&gt; {
    const modalState = useState&lt;TModal&gt;(false);

    return &lt;ModalContext.Provider value={modalState}&gt;{children}&lt;/ModalContext.Provider&gt;;
};

const useModalContext = () =&gt; React.useContext(ModalContext);

When I call useModalContext hook to use in another component I get an error:

> Type 'TModalContext | null' is not an array type.ts(2461)

const [isOpen, setOpen] = useModalContext();

How can I resolve this issue ?

答案1

得分: 1

Solution 1:

因为您告诉编译器 null 是一个有效的值,最简单的方法是在上下文为 null 时抛出一个 Error

const useModalContext = () => {
    const context = React.useContext(ModalContext);
    if(context === null) throw new Error; // 或者在此处执行其他操作
    return context
}

如果您始终在上下文内调用钩子,您就不必担心应用程序因 Error 而崩溃。

Solution 2:

另一方面,如果您无法确保钩子始终安全,您应该考虑处理来自调用者的 null 值:

const context = useModalContext();
const [isOpen, setOpen] = context ?? []
英文:

Solution 1:

Because you tell the compile that null is a valid value, the most simple way is throw an Error when the context is null:

const useModalContext = () =&gt; {
    const context = React.useContext(ModalContext);
    if(context === null) throw new Error; // or do other things here
    return context
}

You won't to worry about the app crashed by the Error if you always call the hook inside the context

Solution 2:

On the other hand, if you can't ensure the hook is always safe, you should consider handle the null value from the caller:

const context = useModalContext();
const [isOpen, setOpen] = context ?? []

huangapple
  • 本文由 发表于 2023年5月13日 20:10:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定