Home > Net >  How can I use props with this data
How can I use props with this data

Time:07-10

so I have no data on videos but there is data on clients. I'm guessing my fallback doesn't work because of it.

how can I separate those data. so I can do something like this while using fallback. if(!videos) return props: null

import HomeComponent from "../components/Home";
import { sanityClient } from "../components/sanity";

export const getServerSideProps = async ({ params }: any) => {
  const query = `{
  'clients': *[ _type == "clients"] {
       _id,
        client,
        mainImage,
  },
  'videos': *[ _type == "videos"] {
     id,
     title,
        url,
  },
  'services': *[ _type == "services"] {
     _id,
        title,
        description,
        mainImage,
  },
}`;
;
    
  const props = await sanityClient.fetch(query);
    if (!props) {
      return {
        props: null,
        notFound: true,
        fallback: "blocking",
      };
    }
  return { props: { props } };
};


const Home= ({ props }: any) => {
  return (
    <>
      <HomeComponent props={props} />
    </>
  );
};

export default Home;

CodePudding user response:

I'm not sure how to interpret your question.

If you are saying that the prop returned by sanityClient.fetch is either falsy or an object that might contain a videos property that may or may not be falsy? then you can test that with

if (props?.videos?.length) {
   // Do your thing here
}
  • Related