Create a new component in a JSS Next.js app using the code-first development workflow
Creating new components is one of the most common tasks performed while developing JSS applications. JSS Next.js applications provide a scaffolding script, defined in the file scripts/scaffold-component.ts to facilitate the creation of new components.
This procedure demonstrates how to use the scaffolding script to create a new component in your JSS Next.js app using the code-first development workflow.
To create a component in a Next.js JSS application:
-
In a terminal, in the root directory of your Next.js project, start the application by running the script:
TipIn your browser, go to http://localhost:3000/ to see your code changes in real-time.
-
Open a new terminal tab/window. In your application's root directory, run:
TipThe scaffolding system emits a simple working component. You can modify the scaffolding conventions in
src/scripts/scaffold-component.js. -
In the application's root directory, in the file
sitecore/definitions/components/MyComponent.sitecore.tscontaining the manifest definition for the new component, define the data fields for the component. For example: -
In the file
src/components/MyComponent.tsx, implement the React component. For example: -
The newly created component is inactive until added to a layout on a route. Add the component to a route layout. In this example, we add the component to the root route to the default placeholder
jss-main. In the file/data/routes/en.yml, add your component to the array of components under thejss-mainplaceholder: -
Check your browser. The new component displays at the top of the page.