2

I have been working on this modern carousel which i found from this reference https://github.com/pnp/sp-dev-fx-webparts/tree/main/samples/react-modern-carousel

But then i come across this issue "Argument of type 'null' is not assignable to parameter of type"

export const WebpartContext = createContext<IAwardRecognitionProps>(null);

IAwardRecognitionProps

export interface IAwardRecognitionProps {
  webpartTitle: string
  contentTitle: string
  contentDescription: string
  isDarkTheme: boolean
  environmentMessage: string
  hasTeamsContext: boolean
  userDisplayName: string
  animationChoice: string
  cardType: string
}

AwardRecognition.tsx

import * as React from "react";
import { IAwardRecognitionProps } from "./IAwardRecognitionProps";
import { FluentProvider, webLightTheme } from "@fluentui/react-components";
import { Carousel } from "../../../components/Carousel"
import { createContext } from "react";

export const WebpartContext = createContext<IAwardRecognitionProps>(null);

export const AwardRecognition = (props: IAwardRecognitionProps): JSX.Element => {
  return (
    <WebpartContext.Provider value={props}>
      <FluentProvider
        theme={webLightTheme}
        style={{ width: "100%", background: "none" }}
      >
        <Carousel />
      </FluentProvider>
    </WebpartContext.Provider>
  );
};

Context.tsx

'contextInfo' is possibly 'undefined'.

import * as React from "react";
import { Text } from "@fluentui/react-components";
import styles from "../webparts/awardRecognition/components/AwardRecognition.module.scss";
import { ContentProps } from "../types/ContentProps"
import { useContext } from "react";
import { WebpartContext } from "../webparts/awardRecognition/components/AwardRecognition";

export const Content = ({ user }: ContentProps): JSX.Element => {
  const contextInfo = useContext(WebpartContext);

  return (
    <section className={styles.contentWrapper}>
      <Text size={600} weight="semibold">
        {contextInfo.webpartTitle}
      </Text>
      <div className={styles.contentDescriptionWrapper}>
        <Text size={400} weight="semibold" className={styles.contentTitle}>
          {contextInfo.contentTitle}
        </Text>
        <Text size={300}>{contextInfo.contentDescription}</Text>
      </div>

      <div className={styles.userInfo}>
        <Text size={800} weight="semibold" className={styles.userName}>
          {user?.Title}
        </Text>
        <Text size={400}>{user?.Designation}</Text>
      </div>
    </section>
  );
};

1 Answer 1

2

The error occurs because TypeScript is strict about the types and does not allow null as an initial value for a context that expects a specific type (IAwardRecognitionProps in your case).

To resolve this, you can provide a default value that matches the expected type. If you don't have an initial value, you can use undefined as an initial value by setting the type to include undefined.

Allow undefined as a possible type:

export const WebpartContext = createContext<IAwardRecognitionProps | undefined>(undefined);

With this you will have to check if property under the object is undefined or not. And for that you can use your return method somewhat like this (I guess it is Optional Chaining concept):

return ( <div> 
{contextInfo?.webpartTitle? 
( <p>{contextInfo.webpartTitle}</p> ) 
: ( <p>Loading...</p> )} </div> );

OR

Define a default value:

// Define a default value that matches IAwardRecognitionProps 
const defaultContext: IAwardRecognitionProps = { 
   // Provide default values for all properties 
   webpartTitle: '', 
   contentTitle: '', 
   // other properties... 
}; 

export const WebpartContext = createContext<IAwardRecognitionProps>(defaultContext);
6
  • If i allow undefined, in content.tsx possibly the contextInfo has the issue of "'contextInfo' is possibly 'undefined'" Could you please advice on this. I have edited the main question, And i do not have initial value defined.
    – striker92
    Commented Nov 20 at 15:23
  • 1
    @striker92: I have updated my answer for the undefined approach. But I would suggest to go with default value approach where you can define default value or empty value. Commented Nov 20 at 15:32
  • 1
    Return method seems to be working on this case, thank you for the advice. But I will try with the default value and update if that works as expected.
    – striker92
    Commented Nov 20 at 15:40
  • I think there is a last breaker to this workbench, Any idea about this error ? Error - [webpack] Error processing webpack stats: TypeError: Cannot read property 'toJson' of undefined Error - [webpack] Webpack error: TypeError: localizedPath.startsWith is not a function
    – striker92
    Commented Nov 20 at 15:52
  • I feel like this is very Highlevel error message. Is there any line number or file names are provided with this to check that location? Are you getting this error while gulp build or gulp bundle? Commented Nov 21 at 6:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.