Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
Next.js App crashed if prop is undefined when destructuring code?

I will try to explain my problem, I have created a custom Context API wrapper for all my data. Now, I have this docType which is not always defined nor existing.

When I destructure it like so:

const { docType } children?.props?.data; // My Next App / JS crashes and getting the undefined error.

Doing this works:

const docType = children.props.data?.docType;

I am not really sure why this happens. destructuring all other data props will fail if I don't write my code like so:

import { createContext, useContext } from "react";

const AppContext = createContext();

export const AppWrapper = ({ children }) => {
  const docType = children.props.data?.docType;
  const site = children.props.data?.site;
  const page = children.props.data?.page;
  const preview = children.props?.preview;

  const sharedState = {
    docType,
    preview,
    page,
    site,
  };

  return (
    <AppContext.Provider value={sharedState}>{children}</AppContext.Provider>
  );
};

export const useAppContext = () => useContext(AppContext);

Can someone explain to me why I am getting this problem?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use like this:

const { docType, site, page, site} = children?.props?.data || {}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!