Skip to main content

SitecoreAI · Tutorial

Making Components Editable in Experience Editor Without a Data Source

Give authors a clickable empty state in Experience Editor when a component has no datasource yet.

GavAusWeb2 min read

One thing that trips up front-end devs new to XMC: if a component's datasource is empty, it just silently renders nothing in Experience Editor, with no way for a content author to click into it and add one. You need to explicitly opt each component into an 'empty hint' fallback.

Render an empty hint while editingLink to this section

Check props.fields and, if it’s missing while in editing mode, render a placeholder block with an is-empty-hint span instead of bailing out to nothing. That gives authors a clickable empty state in the Experience Editor so they can attach a datasource.

HeroComponent.tsxtsx
import { useSitecoreContext } from '@sitecore-jss/sitecore-jss-nextjs';

const HeroComponentDefault = (props: HeroComponentProps): JSX.Element => (
  <div className={`component ${props.params?.styles}`.trimEnd()}>
    <div className="component-content">
      <span className="is-empty-hint">[ Hero Component ]</span>
    </div>
  </div>
);

const HeroComponent = (props: HeroComponentProps): JSX.Element => {
  const { sitecoreContext } = useSitecoreContext();
  const isEditing = sitecoreContext?.pageEditing;

  if (!props.fields) {
    if (isEditing) {
      return <HeroComponentDefault {...props} />;
    }
    return <></>;
  }

  return (
    <div className={`component ${props.params?.styles}`.trimEnd()}>
      {/* real component JSX using props.fields */}
    </div>
  );
};

export default HeroComponent;

Apply it to every droppable componentLink to this section

Apply this pattern to every component that's meant to be droppable onto a page before a datasource exists. It's a small bit of boilerplate, but it's the difference between authors being able to self-serve a new component versus needing a developer to wire up the datasource first.