2

I have a context:

export const templateContext = createContext({
  isButtonDisable: true,
  setIsButtonDisable: (p: boolean) => {},
  isSubmitReady: <boolean>false,
  setIsSubmitReady: () => {},
  buttonShow: false,
  handleButtonShow: (val: boolean) => {},
  steps:  [] ,
  handleSteps: (val: never) => {},
});

I am not understanding How can I define the type of array in this context. especially the steps array. That array also contains object

1 Answer 1

5

You need to add a type for your object first, e.g.:

type MyContext = {
  isButtonDisable: boolean;
  setIsButtonDisable: (p: boolean) => {};
  // and so on //
  steps: String[];
};

If array is complex, you should create a separate type for it as well. For example, you array contains an array of Step objects:

type Step = {
  id: string;
  value: number;
};

So now you can modify your MyContext:

type MyContext = {
  isButtonDisable: boolean;
  setIsButtonDisable: (p: boolean) => {};
  // and so on //
  steps: Step[];
};
Sign up to request clarification or add additional context in comments.

1 Comment

Yup do this and then set the generic when you create the context like createContext<MyContext>({

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.