پیشتر در مطلب «
React 16x - قسمت 33 - React Hooks - بخش 4 - useContext Hook» با هوک useContext آشنا شدیم. در این قسمت میخواهیم نکات تایپاسکریپتی آنرا بررسی کنیم.
ایجاد UserContext
فرض کنید برای انتقال دادههای کاربر وارد شدهی به سیستم، از روش انتقال props از بالاترین کامپوننت موجود در component tree به پایینترین کامپوننت آن، نیاز است از Context استفاده کنیم. به همین جهت فایل جدید src\contexts\userContext.tsx را با محتوای زیر ایجاد میکنیم:
import React from "react";
export type User = {
name: string;
isActive: boolean;
logOut: () => void;
};
export const UserContext = React.createContext<User>({});
export const useUser = () => React.useContext(UserContext);
متد createContext در اصل یک متد جنریک است که بر اساس نوع آرگومان جنریک آن، مقدار شیء تامین کنندهی مقدار آن مشخص میشود. برای مثال اگر نوع User را تعریف کرده و به آن انتساب دهیم، یک چنین امضایی را پیدا میکند:
function React.createContext<User>(defaultValue: User,
calculateChangedBits?: ((prev: User, next: User) => number) | undefined): React.Context<User>
اما ... قطعه کد فوق با خطای تایپاسکریپتی زیر کامپایل نمیشود:
Argument of type '{}' is not assignable to parameter of type 'User'.
Type '{}' is missing the following properties from type 'User': name, isActive, logOutts(2345)
عنوان میکند که شیء خالی که به createContext ارسال کردهایم، از نوع User نیست. برای رفع این مشکل میتوان از مفهومی به نام Partial استفاده کرد:
export const UserContext = React.createContext<Partial<User>>({});
به این ترتیب تمام خواص نوع User به صورت اختیاری معرفی میشوند و در این حالت انتساب یک شیء خالی اولیه به آن، مشکلی را ایجاد نخواهد کرد.
نکته 1: البته میتوان آرگومان جنریک آنرا ذکر نکرد و createContext را با یک شیء آغازین از نوع User مقدار دهی کرد. در این حالت با استفاده از Type Inference، نوع آن، همان User درنظر گرفته میشود و دیگر نیازی به ذکر صریح این نوع نخواهد بود.
نکته 2: اگر از شیء مقدار دهی شدهی آغازین استفاده کنید، دیگر حتی نیازی به ذکر export type User هم نیست. نوع createContext بر اساس نوع خواص شیء آغازین ارسالی به آن در سراسر برنامه مشخص شده و قابل دسترسی میشود؛ با intellisense کامل و type checking دقیق.
نکته 3: useUser تعریف شده، در حقیقت یک هوک سفارشی است که میتوان بجای React.useContext از آن در سایر قسمتهای برنامه استفاده کرد.
نکته 4: اگر علاقمند به استفادهی از نوع Partial نیستید، میتوان از union types هم در اینجا استفاده کرد. در این حالت میتوان مقدار اولیه را undefined تعریف کرد:
export const UserContext = React.createContext<User | undefined>(undefined);
معرفی UserContext به بالاترین کامپوننت موجود در component tree
برای این منظور به فایل src\App.tsx مراجعه کرده و آنرا با UserContext.Provider محصور میکنیم:
import { User, UserContext } from "./contexts/userContext";
// ...
function App() {
const user: User = {
name: "User 1",
isActive: true,
logOut: () => {
console.log("LogOut.");
},
};
return (
<UserContext.Provider value={user}>
// ...
</UserContext.Provider>
);
}
export default App;
ابتدا نوع User و سپس UserContext را import کردهایم و سپس کل محتوای موجود در کامپوننت App را با UserContext.Provider که دارای مقدار user ابتدایی تعریف شدهاست، محصور میکنیم.
خواندن شیء Context در کامپوننتی دیگر
برای این منظور به کامپوننت src\components\Head.tsx که در قسمتهای قبل ایجاد کردیم، مراجعه کرده و آنرا به صورت زیر تکمیل میکنیم:
import { UserContext } from "../contexts/userContext";
// ...
export const Head = ({ title, isActive = true }: Props) => {
const context = React.useContext(UserContext); // or useUser();
// ...
};
در اینجا با استفاده از هوک useContext به UserContext دریافتی دسترسی یافته و سپس میتوان با اطلاعات شیء User کار کرد.
اولین مزیت کار با TypeScript در اینجا، وجود intellisense کامل به همراه context است:
و یا اگر از Object Destructuring استفاده کنیم، در صورت وجود یک اشتباه تایپی، بلافاصله در زمان کامپایل، خطایی را دریافت میکنیم:
یک نکته: اگر UserContext را با استفاده از union types تعریف کنیم:
export const UserContext = React.createContext<User | undefined>(undefined);
هنگام استفادهی از آن، خطای عدم وجود خاصیتهای آنرا در حین Object Destructuring دریافت میکنیم:
علت اینجا است که با فعال بودن بررسی نوعهای نالنپذیر، چون user context اکنون میتواند undefined هم باشد، یا باید از روش context?.name استفاده کرد و یا یک علامت تعجب را پس از useContext قرار داد:
const context = React.useContext(UserContext)!; // or useUser()!;
const { name } = context;
ذکر ! به این معنا است که میدانیم خروجی این متد، نال و یا undefined نیست (راهنمایی کردن کامپایلر TypeScript). چون پیشتر آنرا در کامپوننت App، در حین تعریف Provider، مقدار دهی اولیه کردهایم.