Sknoe / Notes

React for Salesforce Storefront Next: The Concepts SFRA Developers Need

Already understand SFRA but new to React? Learn components, props, state, events, forms, and how they fit into Storefront Next.

10 min read

You have learned the JavaScript patterns that show up in React: objects, arrays, callbacks, map(), and functions. But knowing the syntax does not yet answer the questions that make a React screen feel unfamiliar. Who owns the product data? What changes when someone clicks a button? Why does calling a setter make the screen update? And where does the server fit when the browser is not doing all the work?

Overwhelmed? Wait I have a solution to help wrap your head around this.

If you come from SFRA, these questions may sound familiar in a different form.

This guide gives you the React pieces you need to follow that flow without asking you to learn every React feature at once.

As always we will lean on examples to get brains familiar with these concepts. We will use one product page and one add-to-cart interaction throughout. First, we will read a component. Then we will see how props, state, events, lists, and forms fit together. At the end, we will connect those ideas to Storefront Next loaders and actions. Sounds good?

Salesforce documents Storefront Next as using React 19 with React Router 7 framework mode; its server-side loaders and actions have responsibilities that should not be confused with local component state.

What is React doing when it renders a page?

React lets you describe the interface as components. If you got this then that is all you needed. Adios :D. Kidding! But that is all that is there to react- Components! A component receives inputs, calculates what its section of the interface should look like, and returns JSX. JSX is the markup-like syntax you saw in the JavaScript guide. React reads the returned description and updates the page.

The useful analogy here is a product-card design template. A product-card component describes how one card should look; each use of that component receives the data for one product. The analogy helps explain reusable components and their inputs, but it should not be stretched to explain server requests, user sessions, or every part of React state.

That distinction matters: you write a component to say what the UI should be for its current inputs. You do not normally call the component function yourself or edit the DOM by hand. React manages when components render and applies the result to the page.

A simple React data path
  1. Route data arrives

    Storefront Next loads product data for the current route.

  2. A parent receives it

    The route component reads the loaded product.

  3. Props pass data down

    The parent supplies product details to child components.

  4. Components return JSX

    Each component describes its part of the interface.

  5. React renders the page

    The browser receives server-rendered HTML and React makes it interactive.

1. What is a React component?

A component is a reusable part of the interface. In modern React, components are commonly JavaScript functions whose names start with a capital letter. A component can return an element, include other components, and use values passed in as props.

ProductCard.jsx
function ProductCard({ product }) {
return (
<article>
<h2>{product.name}</h2>
<p>{product.price}</p>
<button type="button">Add to cart</button>
</article>
);
}

ProductCard is a function component. It receives a product prop and returns JSX describing an article. The braces around product.name and product.price mean “evaluate this JavaScript expression here.” The JSX is not a separate template language; it is syntax written inside the JavaScript file.

React component names start with a capital letter, such as <ProductCard />. Lowercase names such as <article> refer to built-in browser elements. That naming convention lets React distinguish your components from HTML elements.

2. What are props in React?

Props are the inputs a parent component passes to a child component. In the example below, ProductPage owns the product value and passes it to ProductCard. The child reads the prop to display the name and price.

jsx
function ProductPage({ product }) {
return (
<main>
<h1>Product details</h1>
<ProductCard product={product} />
</main>
);
}
function ProductCard({ product }) {
return (
<article>
<h2>{product.name}</h2>
<p>{product.price}</p>
</article>
);
}

The parent decides which product value to pass. The child does not reach into the parent to fetch it. This gives us a clear direction for data: from a parent down to its children. Treat props as read-only. If a button needs to tell the parent that a shopper clicked it, the parent can pass a callback prop for the child to call.

jsx
function ProductCard({ product, onAddToCart }) {
return (
<article>
<h2>{product.name}</h2>
<button onClick={() => onAddToCart(product.id)}>
Add to cart
</button>
</article>
);
}

Here the parent passes onAddToCart, and the child calls it with the product ID after a click. The callback communicates an event upward; it does not mean the child owns the cart or changes the server basket itself.

3. How does React render a list of products?

A product listing page often starts with an array. JavaScript map() turns each product into a component element, while React renders the elements. Give each sibling element a stable key so React can match it to the same item when the list changes.

jsx
function ProductGrid({ products }) {
return (
<ul>
{products.map((product) => (
<li key={product.id}>
<ProductCard product={product} />
</li>
))}
</ul>
);
}

The key is for React’s list handling. It is not shown on the page and is not passed to ProductCard as a prop. If the child needs the product ID, pass product.id separately. Use a stable ID from the data instead of an array index when items can be inserted, removed, or reordered.

Your previous JavaScript article introduced map() and keys. Here the new part is how the returned items become nested components: ProductGrid makes one <li> per product and places a ProductCard inside it.

4. What is state in React?

State is information a component keeps between renders. A quantity selector, open/closed accordion, or temporary message can be local state. In a function component, the useState Hook gives you the current value and a setter that asks React to render again with the new value.

jsx
import { useState } from 'react';
function QuantityPicker() {
const [quantity, setQuantity] = useState(1);
function increase() {
setQuantity((currentQuantity) => currentQuantity + 1);
}
return (
<div>
<button type="button" onClick={increase}>+</button>
<span>{quantity}</span>
</div>
);
}

quantity is the value for this render. setQuantity asks React to render again using the new value. You do not assign directly to quantity, and calling setQuantity does not change the value in the already-running function. React will provide the updated value on a later render.

The updater form setQuantity((currentQuantity) => currentQuantity + 1) is useful when the next value depends on the previous one. React gives the updater the latest state value. This avoids calculating from a stale variable when updates are queued close together.

5. What happens when a shopper clicks a button?

An event handler is a function that runs in response to an interaction, such as a click or form submission. In JSX, you pass the function to an event prop such as onClick; you do not call it while rendering.

jsx
function FavoriteButton() {
const [saved, setSaved] = useState(false);
function handleClick() {
setSaved((currentSaved) => !currentSaved);
}
return (
<button type="button" onClick={handleClick}>
{saved ? 'Saved' : 'Save for later'}
</button>
);
}

onClick={handleClick} gives React the function to call later. Writing onClick={handleClick()} would call the function while React is rendering the button. The returned label changes because the handler updates state and React renders again with the new saved value.

For local UI behavior, such as switching a label or opening a panel, component state may be enough. For an actual add-to-cart operation, the server-side route and Commerce API need to process the mutation. A button click can start that request, but local state alone does not prove that the basket changed.

6. How do forms work in React?

A form lets the shopper provide a value, such as a quantity. A controlled input gets its displayed value from React state and reports edits through an event handler. This is useful when the interface needs immediate validation or dependent UI, but not every input must be controlled.

jsx
function QuantityField() {
const [quantity, setQuantity] = useState(1);
function handleChange(event) {
setQuantity(Number(event.target.value));
}
return (
<label>
Quantity
<input
type="number"
min="1"
value={quantity}
onChange={handleChange}
/>
</label>
);
}

The value prop keeps the input display aligned with quantity, and onChange updates that state when the shopper types. The min attribute improves the browser’s input experience, but server-side validation is still required. A browser is not a trusted place to enforce commerce rules.

Storefront Next adds a route-based form flow on top of React. A React Router <Form> can submit form data to the route’s action; a useFetcher() form can submit without navigating away. The route action handles the server-side mutation and React Router can revalidate route data afterward. We will use that pattern in the add-to-cart tutorial rather than pretending this local quantity state is the basket.

7. How should a React component show loading and error states?

The interface should reflect the result of an operation. A component can display a loading label while a request is being submitted, show a message if the request fails, and render the updated cart state after the server responds. These are conditional renderings: regular JavaScript conditions decide which JSX to return.

jsx
function AddToCartButton({ status, error }) {
if (status === 'submitting') {
return <button disabled>Adding…</button>;
}
return (
<>
<button type="submit">Add to cart</button>
{error ? <p role="alert">{error}</p> : null}
</>
);
}

This small component displays the status passed to it. In a Storefront Next route, a fetcher exposes its submission state and returned data, so the parent can pass those values into a button or message component. Avoid showing a success message before the server confirms success.

8. Do I need useEffect to fetch data in React?

Not for the main page data in Storefront Next. React’s useEffect is for synchronizing a component with an external system after rendering, such as setting up a browser event listener or focusing an input. It is usually the wrong tool for calculating a value from props or state, and Storefront Next’s docs recommend server-side loaders for initial route data.

A route loader runs on the server before its component renders. The component reads the returned data. An action handles a mutation, such as a form submission. A fetcher can call a loader or action without changing the URL. Those are React Router features used by Storefront Next; they are not React Hooks.

tsx
// Simplified route-module example; Commerce API details omitted
export async function loader({ params }) {
const product = await getProductFromCommerce(params.productId);
return { product };
}
export default function ProductRoute() {
const { product } = useLoaderData<typeof loader>();
return <ProductPage product={product} />;
}

This is the important division: the loader gets route data; ProductRoute and its children describe the UI for that data. That may feel familiar from SFRA’s separation between route work and ISML rendering, although the files and APIs are different. In Storefront Next, the initial response is server-rendered, then React hydrates the page so browser interactions work. Do not treat React as “browser only.”

Under the hoodEffects are for synchronization, not ordinary data flowAn Effect is an escape hatch for connecting React to something outside React.

Looking closer

Use `useEffect` when a component must synchronize with a browser API or an external subscription. Do not add an Effect just to derive `fullName` from two state values, or to copy a prop into another state variable. For Storefront Next page data, use the route loader; for form mutations, use the route action or a fetcher. Salesforce’s Storefront Next guidance uses this server-first route lifecycle, while React’s own documentation says many Effects can be removed when they only transform data or respond to an event.

How does this compare with SFRA?

The comparison is useful as a starting point, not a one-to-one translation. In SFRA, a controller route receives the request and prepares data for a template; ISML renders the response using view data such as pdict. In Storefront Next, the route module connects a URL to loaders/actions and a React component tree. A loader prepares route data, components use props to display it, and an action handles a server-side mutation.

The main React learning goal is to understand the UI side of that flow: a component receives inputs, returns JSX, and can request a re-render when its local state changes. The Storefront Next learning goal is to understand where data and mutations belong in the route lifecycle. The next article in this series will map those responsibilities from SFRA to Storefront Next in more detail.

Reported interview questions and practice

The questions below separate public candidate-reported interview prompts from extra practice questions. A report shows that a person said the question came up in an interview; it does not establish that employers ask it universally or that it is a “most common” question. The source accounts are linked in Further Reading.

What to read next

  • JavaScript for React: A Practical Guide for SFRA Developers

    Builds on the previous article

    Review objects, callbacks, arrays, destructuring, modules, and async JavaScript before continuing.

  • SFRA Models and ViewModels

    Connects data preparation to rendering

    Recall how SFRA shapes data for its templates before comparing that flow with route loaders and React components.

  • SFRA to Storefront Next: routes, data, and rendering

    Next article in this series

    Map controllers, ISML, server data, and rendering responsibilities across the two storefront architectures.

  • Build Add to Cart in Storefront Next

    Apply React to a complete feature

    Follow a product page through a route action and Commerce API response to the shopper-facing result.

Did this click?

Tap the bulb if this explanation helped.

Further reading

Continue this learning path

The SFRA request flow is covered in How SFRA Works: A Beginner’s Guide, and data preparation in SFRA Models and ViewModels: How Data Gets Ready for ISML. For a complete SFRA feature, follow Build Add to Cart: A Backend-to-Frontend SFRA Journey. Next, compare the architecture in SFRA to Storefront Next: How Routes, Data, and Rendering Change.

Interview questions

How would you share state between components that need the same data?

First locate the closest common parent and keep the state there, then pass the value and any update callback to children through props. For wider shared state, context or a state library can be appropriate, but choose the simplest mechanism that fits the component tree and update needs.

How could you coordinate state across components if you could not use React or a third-party state library?

One option is a small shared store or an event-based mechanism, such as dispatching and listening for CustomEvents. The right answer depends on the application; explain who owns the data, how updates are announced, and how views subscribe and clean up.

How can a child component send information to its parent?

The parent passes a callback as a prop. The child calls that callback when the event occurs and passes any needed value, such as a product ID. The parent remains the owner of the state or operation.

What is the difference between a controlled and an uncontrolled input?

A controlled input gets its current value from React state and reports changes through an event handler. An uncontrolled input keeps its current value in the DOM, and code reads it when needed, often using a ref or form submission. Use the approach that fits the interaction; controlled is useful when the UI must respond to each edit.

Why does calling a state setter not change the current state variable immediately?

A render sees a snapshot of state. Calling its setter queues an update and asks React to render again; the current function continues to see the value from its own render. Use an updater function when the next value depends on the previous value.

Should a Storefront Next product route fetch its main data in `useEffect`?

No. Storefront Next uses a server-side route loader to fetch initial route data before rendering. `useEffect` is for synchronizing with external systems after a component renders; actions and fetchers handle route mutations and in-place requests.