"Please delete this question — issue resolved."
"Nothing was returned from render" in introducing props module.
I encountered a runtime error while working through the Microsoft Learn module titled "Introducing props in React." In this module, we create a component called RecipeTitle and pass the recipe title to it from the App component using props. I followed the instructions to create the RecipeTitle component, which correctly accepts a props parameter and returns JSX displaying the title in an <h2> tag. However, after updating the App.jsx file as instructed, I received the following error in the browser: "Unhandled Runtime Error: App(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null."
After reviewing my code, I realized that the error was caused because the App function was not returning any JSX. Although I had written <RecipeTitle title={recipe.title} /> in the function body, I had not wrapped it in a return statement. In React, a functional component must return either JSX, null, or another component. Without a return statement, the JSX is ignored, which causes the error I experienced.
To fix the issue, I modified the App component to include a return statement and wrapped the JSX inside a <div>. After doing this, the error was resolved, and the title "Mashed potatoes" appeared correctly on the page. This was a helpful reminder that in functional components, JSX must always be explicitly returned for the component to render correctly. I’m posting this in case others run into the same issue while following the Microsoft Learn tutorial.
Here is the correct code for the App.jsx
import React from "react";
import RecipeTitle from "./RecipeTitle"
import './index.css'
function App() {
const recipe = {
title: 'Mashed potatoes',
feedback: {
rating: 4.8,
reviews: 20
},
ingredients: [
{ name: '3 potatoes, cut into 1/2" pieces', prepared: false },
{ name: '4 Tbsp butter', prepared: false },
{ name: '1/8 cup heavy cream', prepared: false },
{ name: 'Salt', prepared: true },
{ name: 'Pepper', prepared: true },
],
};
return (
<div>
<RecipeTitle title={recipe.title} />
</div>
);
};
export default App;