Error: The render function in ReactJS components did not return anything

While attempting to create a new component and call it within App.jsx (another component) for rendering, an error is being encountered.

Error: Error: App(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.

Below is the code I have written:

Component: (Greetings.Jsx)

import React from 'react'
function Greetings()
{
    let timehours=new Date()
    timehours=timehours.getHours();
    let cssStyle={
        color:'Green',
        
      }
      
      let text="";
      if(timehours>=1&&timehours<=12)
      {
       
        // z.src = window.location.origin + '/images/morning.jpeg'; 
        // document.body.backgroundImage= z.src;
        text="Good Morning";
        cssStyle.color='Green';
        // bgimg.Image=window.location.origin + '/images/morning.jpg';
        
        
      }
      else if(timehours>=12&&timehours<19)
      {
        
        // bgimg.Image=window.location.origin + '/images/morning.jpg';
        text="Good Afternoon";
        cssStyle.color='Orange';
      }
      else
      {
        text="Good Night";
        cssStyle.color='Black';
      }

      return(
        <>
            <div>
                <h1>Hello Sir, <span style={cssStyle}>{text}</span></h1>
            </div>  
        </>
      );
}
export default Greetings;

App.Jsx


import React from 'react';
import Greetings from './Greetings'

function App()
{
    return 
    (
        <>
            <Greetings/>
        </>
    );
}

export default App;

Index.jsx

import React from 'react';
import reactDom from 'react-dom';
import ReactDOM from 'react-dom';
import "./index.css";
import App from "./App";


ReactDOM.render(<App/>,document.getElementById("root"));

Answer №1

I recently revamped the App.jsx file:

import React from 'react'

import Greetings from './Greetings'

function App()
{
    return(<Greetings></Greetings>);
}

export default App;

This update has resolved my issue!

Answer №2

It is unnecessary to have empty brackets within the code, and in App.Jsx, the parentheses following the return statement should be on the same line (special shoutout to @Martin):

Greeting.jsx:

import React from 'react'

function Greetings()
{
    // additional code is not shown for brevity    
    return(            
        <div>
            <h1>   Hello Sir, <span style={cssStyle}>{text}</span></h1>
        </div> 
    );
}
export default Greetings;

and:

import React from 'react';
import Greetings from './Greetings'

function App()
{
    return <Greetings/>;
}

export default App;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Easily manipulate textboxes by dynamically adding or deleting them and then sending the data to a

Can you provide a simple example of how to dynamically add and remove textboxes and then display the results? I envision it like this: [Empty Textbox][Add button] When 'Value1' is entered into the textbox and 'add' is clicked, it s ...

Should we opt for Controlled components or Uncontrolled components when it comes to working with HTML forms in React

I am currently faced with a decision on where the best location is for housing my Form State. The React documentation states: Identify every component that renders something based on that state.  Find a common owner component (a single component above ...

incapable of acquiring/displaying the entered data

I'm attempting to update a user's name in React by filling out an input field and clicking a button. However, my code is not functioning as expected. I am fairly new to React, so any help would be appreciated! Here's what my code looks like ...

JQuery Accordion SubMenu/Nested Feature malfunctioning

I have successfully implemented a JQuery Accordion on my website and it is functioning properly, opening and closing as expected. However, I am facing an issue when trying to add a submenu within the accordion. The submenu does not work as intended and lac ...

When the user clicks on a specific element, ensure that it is the main focus and generate an overlay

One of my challenges is implementing a custom element that captures user input upon clicking, focusing on it and overlaying other elements. I want the overlay to disappear if the user clicks outside the div. I attempted to achieve this using the iron-over ...

Integrate Arduino's capabilities with Johnny-Five within an HTML JavaScript document

As a beginner in the world of JavaScript, I set out to embark on a project involving turning on a connected LED on an Arduino board by simply pressing a button on an HTML page. After doing some research, I came across a JavaScript library called "johnny- ...

"Retrieve the position of a contenteditable element while also considering HTML

I've been exploring a method to generate annotations within HTML text using JavaScript. The approach involves the user clicking on a contenteditable <div> and obtaining the cursor's position based on their click. Subsequently, when insertin ...

Tips for integrating Atomic Design principles into Angular development

Can the combination of Angular and atomic design be a beneficial choice? "There is much discussion on creating design systems, with emphasis on defining color schemes, typography, grids, textures, etc. While these aspects are crucial, I am more focused on ...

Use the nobr HTML tag on Django form fields

I have been customizing Django form fields using this helpful guide. Here is the form structure: class contact(forms.Form): first_name = forms.CharField() middle_name = forms.CharField() last_name = forms.CharField() This is how the template ...

What is the best way to determine the total number of rows in a JSON file?

Here is the JSON data I have: [0:{name:"jason",height:"150cm"}, 1:{name:"henry",height:"178cm"}] In my function, I am attempting to create a for loop like this: function DrawTable(output) { var ...

The Trans component from next-translate is not functioning as expected when attempting to replicate the usage from the example

I've been trying out next.js and incorporating the next-translate library to add i18n support to my website. One challenge I've encountered is implementing an example in my website. Below is a snippet of my .jsx code: <p> <Trans i1 ...

Having trouble retrieving data from JSON using JavaScript

Hey folks, I need some help with the following code snippet: function retrieveClientIP() { $.getJSON("http://192.168.127.2/getipclient.php?callback=?", function(json) { eval(json.ip); });} This function is used to fetch the IP address of visitors. When i ...

Personalized IconComponent within MuiSelect

Can the default IconComponent="ArrowDropDownIcon" be replaced with CustomIcon using props within createMuiTheme? I am hoping to achieve something similar to the following: const theme = createMuiTheme({ ... props: { MuiSelect: { ...

Step-by-step guide on programmatically closing the Material-UI DropDownMenu

http://www.material-ui.com/#/components/dropdown-menu Having encountered a styling issue, I was compelled to rearrange the order of components within my material-ui DropdownMenu. Nevertheless, some buttons (renderNavLink) are now failing to close the Dro ...

Determine the byte size of the ImageData Object

Snippet: // Generate a blank canvas let canvas = document.createElement('canvas'); canvas.width = 100; canvas.height = 100; document.body.appendChild(canvas); // Access the drawing context let ctx = canvas.getContext('2d'); // Extrac ...

jquery global variable not working as expected

I'm having trouble making some variables global outside the jQuery function. I've tried using 'var' to declare them first and then assigning values, but when I try to log() them at the end, I get undefined. var lat, lon; $.get('ip ...

`It is important to note that in Tailwind CSS, `h-8` does not supersede `h-4

I developed an Icon component that looks like this: import { IconProp, library } from "@fortawesome/fontawesome-svg-core"; import { far } from "@fortawesome/free-regular-svg-icons"; import { fas } from "@fortawesome/free-solid-svg- ...

Could the `<script src="show.js"></script>` code pose a threat?

Upon delivering a webpage, a software engineer included the subsequent line of code towards the end: <script src="show.js"></script> We are uncertain if adding this code to our webpage poses any risks. What could be the legitimate reason behi ...

Retrieve the user information from Auth0 within the NestJS application

I am currently working on implementing Auth0 authorization in NestJS, but I am unsure of how to retrieve the user's data within the callback URL handler. In a normal express function, this issue could be resolved using the following code. The passpor ...

Redirecting JavaScript form to search engine

I am struggling with creating a form that enables a user to input text and then directs them to a specified search engine with the input as the query. I am encountering difficulties in getting the JavaScript to properly redirect. An interesting observatio ...