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

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

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

  1. import React from 'react';
  2. type TModal = boolean;
  3. type TModalContext = [TModal, React.Dispatch<React.SetStateAction<TModal>>];
  4. const ModalContext = React.createContext<TModalContext | null>(null); // allow null as default value
  5. const ModalProvider = ({ children }: { children: React.ReactNode }) => {
  6. const modalState = useState<TModal>(false);
  7. return <ModalContext.Provider value={modalState}>{children}</ModalContext.Provider>;
  8. };
  9. 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

  1. import React from &#39;react&#39;;
  2. type TModal = boolean;
  3. type TModalContext = [TModal, React.Dispatch&lt;React.SetStateAction&lt;TModal&gt;&gt;];
  4. const ModalContext = React.createContext&lt;TModalContext | null&gt;(null); // allow null as defaut value
  5. const ModalProvider = ({ children }: { children: React.ReactNode }) =&gt; {
  6. const modalState = useState&lt;TModal&gt;(false);
  7. return &lt;ModalContext.Provider value={modalState}&gt;{children}&lt;/ModalContext.Provider&gt;;
  8. };
  9. 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)

  1. const [isOpen, setOpen] = useModalContext();

How can I resolve this issue ?

答案1

得分: 1

Solution 1:

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

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

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

Solution 2:

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

  1. const context = useModalContext();
  2. 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:

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

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:

  1. const context = useModalContext();
  2. 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:

确定