126

I have a component in React which I am importing in index.js, but it is giving this error:

Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null

index.js:

import React from 'react';
import ReactDOM from 'react-dom'; 
import  Search_Bar from './components/search_bar';

const   API_KEY = 'AIzaSyCnpDObOLiiqN87YKJKZ-cxzdAsvYD1F-U';

const App = () => {
    return
    (
        <div>
            <Search_Bar />
         </div>
    );
}

ReactDOM.render(<App />, document.querySelector('#root'));

component:

import React from 'react';

const Search_Bar = () =>
{
    return <input />;
};

export default Search_Bar;
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
pKay
  • 1,832
  • 4
  • 16
  • 23

19 Answers19

194

I had same problem in the render() method. The problem comes when you return from render() as :

render() {
    return 
    (
        <div>Here comes JSX !</div>
    );
}

i.e. if you start the parenthesis in a new line

Try using:

render() {
    return (
        <div>Here comes JSX !</div>
    );
}

This will solve the error

devsourav
  • 2,449
  • 1
  • 11
  • 18
  • 26
    Wow, can't believe there is no compiler warning for this. Thanks, that extra newline formatting was the problem. – TOBlender Mar 30 '18 at 19:24
  • 2
    such a simple solution for something I was struggling for hours. Reviewed my code so many times. – Aklank Jain Apr 07 '18 at 06:16
  • 13
    I suspect that this is a result of automatic semicolon insertion. It's probably parsing the first example as "return; (...);" – bp. Apr 20 '18 at 13:31
  • 3
    Hahaha I just wasted so much time digging through redux form code and wondering if the 'connect' function has somehow changed in the latest version. – kunal pareek May 30 '18 at 13:29
  • 1
    This is such a weird issue, I love to put parentheses and curly braces on new lines to make my code cleaner, I hope there is some kind of work-around for this in React. – Mirza Sisic Nov 21 '18 at 20:38
  • I was returning `return;` from an if statement. – Farhan Aug 19 '19 at 07:37
  • 2
    This issue ate my 30 minutes, thanks for solving this. – Shek Nov 25 '20 at 20:19
  • Would `'use strict';` help here too? – knittl Dec 08 '20 at 12:39
  • Oh no such a simple solution but I struggled for hours to day for it and I was wondering and using class components and then function and no error but the error you mentioned sir but the react native community must fix this because this isn't an error.We have habits of it coming from c++ background so how they can tell this an error.Amazing – bilalmohib Dec 23 '20 at 04:25
  • This also solved my issue. Bravo – Michael K Apr 30 '21 at 18:28
  • someone write a VSCode plugin to detect this onSave ...should be easy enough to grep this manually without having to use a CodeDom ? just brute force it? – snowcode Jul 21 '21 at 16:30
  • Gotcha!... I struggled for an hour to fix the issue as there was not an issue actually. Just a formatting game. Oh God! You have saved my time. – Rohit Khajuria Oct 21 '22 at 10:49
54

Given that you are using a stateless component as a arrow function the content needs to get in parenthesis "()" instead of brackets "{}" and you have to remove the return function.

const Search_Bar= () => (
    <input />; 
);
Joel Santos
  • 569
  • 4
  • 3
  • 2
    This solved my problem. In the component I was routing to I had brackets after my => arrow instead of parenthesis. Too used to standard arrow functions – SeanMC Aug 29 '18 at 02:43
  • This fixed my problem. But could you please explain why it is happening? It seems to me that we are not using an arrow function. – Sapthaka Jan 07 '22 at 11:31
18

the problem is in the return, try this:

return(
);

this solved my problem

15

This error can be seen for a number of reasons:

  1. As mentioned above, starting the parenthesis on a new line.

Error:

render() {
  return  
  (
    <div>I am demo data</div>
  )
}

Correct way to implement render:

render() {
  return (
    <div>I am demo html</div>
  );
}

In the above example, the semicolon at the end of the return statement will not make any difference.

  1. It can also be caused due to the wrong brackets used in the routing:

Error:

export default () => {
  <BrowserRouter>
    <Switch>
      <Route exact path='/' component={ DemoComponent } />
    </Switch>
  </BrowserRouter>
}

Correct way:

export default () => (
  <BrowserRouter>
    <Switch>
      <Route exact path='/' component={ DemoComponent } />
    </Switch>
  </BrowserRouter>
)

In the error example, we have used curly braces but we have to use round brackets instead. This also gives the same error.

Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
Nabhdeep Singh
  • 151
  • 1
  • 2
11

I ran into this error when running Jest tests. One of the components was being mocked in the setup file, so when I attempted to use the actual component in a test, I was getting very unexpected results.

jest.mock("components/MyComponent", () => ({ children }) => children);

Removing this mock (which wasn't actually needed) fixed my issue immediately.

Perhaps this will save you a few hours of research when you know you're returning from your component correctly.

philthathril
  • 476
  • 5
  • 8
  • 4
    This is exactly what my issue was! As a side note, if you want that kind of mock to still be there, I fixed mine by changing it to: jest.mock("components/MyComponent", () => ({ children }) => <>{children}>); As far as I can tell, that should be a safer version of the snippet above, working fine regardless of children being populated or not. – DJ_R Dec 10 '20 at 14:26
  • @DJ_R Thanks for your comment ! <>{children}> was exactly the right solution – Hakim Apr 22 '21 at 10:16
9

I had the same problem with nothing was returned from render.

It turns out that my code issue with curly braces {}. I wrote my code like this:

import React from 'react';

const Header = () => {
    <nav class="navbar"></nav>
}

export default Header;

It must be within ():

import React from 'react';

const Header = () => (
    <nav class="navbar"></nav>
);

export default Header;
Harshit
  • 1,510
  • 19
  • 42
nambk
  • 445
  • 3
  • 13
  • 1
    Thanks, man! This really helped me to fix the issue. I was doing the same thing & I came across your answer which was indeed really helpful :) – Harshit Jan 05 '21 at 19:24
7

We had a component enclosed in the curly braces i.e. { and }:

const SomeComponent = () => {<div> Some Component Page </div>}

Replacing them with the round brackets i.e. ( and ) fixed the issue:

const SomeComponent = () => (<div> Some Component Page </div>)
Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
7

I came across this thread in search of an answer to this error.

The odd thing, for me, was that everything worked while running in dev (npm start), but the error would happen when the app was built (npm run build) and then run with serve -s build.

It turns out that if you have comments in the render block, like the below, it will cause the error:

ReactDOM.render(
  <React.StrictMode>
    // this will cause an error!
    <Provider store={store}>
      <AppRouter />
    </Provider>
  </React.StrictMode>,
  document.getElementById("root")
);

I'm sharing in case someone else comes across this thread with the same issue.

Kim
  • 856
  • 1
  • 11
  • 21
5

Got the answer: I should not use parentheses after return (). This works:

return  <div> <Search_Bar /> </div>

If you want to write multiline, then return ( ...

Your starting parenthesis should be on the same line as return.

fmw42
  • 46,825
  • 10
  • 62
  • 80
pKay
  • 1,832
  • 4
  • 16
  • 23
3

The problem seems to be the parentheses on return. Parentheses should be in line with return statement like this:

return(
)

but not this way:

return
(
)  
besartm
  • 558
  • 1
  • 7
  • 14
2

In my case this very same error was caused by the way I was importing my custom component from the caller class i.e. I was doing

import {MyComponent} from './components/MyComponent'

instead of

import MyComponent from './components/MyComponent'

using the latter solved the issue.

Tom
  • 92
  • 13
2

Another way this issue can pop up on your screen is when you give a condition and insert the return inside of it. If the condition is not satisfied, then there is nothing to return. Hence the error.

export default function Component({ yourCondition }) {

    if(yourCondition === "something") {
        return(
            This will throw this error if this condition is false.
        );
    }
}

All that I did was to insert an outer return with null and it worked fine again.

Luis Febro
  • 1,733
  • 1
  • 16
  • 21
1

I got this error message but was a really basic mistake, I had copy/pasted another Component as a template, removed everything from render() then imported it and added it to the parent JSX but hadn't yet renamed the component class. So then the error looked like it was coming from another component which I spent a while trying to debug it before working out it wasn't actually that Component throwing the error! Would have been helpful to have the filename as part of the error message. Hope this helps someone.

Oh a side note note I'm not sure anyone mentioned that returning undefined will throw the error:

render() {
  return this.foo // Where foo is undefined.
}

Simon Hutchison
  • 2,949
  • 1
  • 33
  • 32
  • You are right @Simon. This resolved my case. I have to provide an option(using logical OR operator) to return null just In case children({ entity }) returns undefined. See below ``` render() { const { children, entity } = this.props; return children({ entity }) || null; } ``` – Sulaiman Abiodun Sep 02 '20 at 20:11
0

write parenthesis next to return not in next line.

Incorrect

return
(
statement1
statement2
.......
.......
)

Correct

return(
statement1
statement2
.........
.........
)
General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • 2
    How does that make a difference? Would work the same way either way – FortyTwo Mar 14 '19 at 08:48
  • 2
    It's because automatic semicolon insertion in JavaScript. More on this here: http://www.bradoncode.com/blog/2015/08/26/javascript-semi-colon-insertion/ – Rafael Rozon Aug 28 '19 at 18:16
0

I have the same error only on the production build. In development was all right, no warning.

The problem was a comment line

ERROR

return ( // comment
  <div>foo</div>
)

OK

// comment
return (
  <div>foo</div>
)
Manuel
  • 37
  • 8
0

That might be because you would be using functional component and in that you would be using these '{}' braces instead of '()' Example : const Main = () => ( ... then your code ... ). Basically, you will have to wrap up your code within these brackets '()'.

0

Had the same error in my functional Component as

function ShopCard(props) {
const { shops = {} } = useContext(ContextProvider);
return(
    shops.map((shop)=>{
    <div></div>     
})
)
}

Instead of

function ShopCard(props) {
  const { shops = {} } = useContext(ContextProvider);
  shops.map((shop)=>{
    return(
   <div></div>

   )            
    })

}
-1

Same error, different situation. I'm posting this here because someone might be in same situation as mine.

I was using context API like below.

export const withDB = Component => props => {
    <DBContext.Consumer>
        {db => <Component {...props} db={db} />}
    </DBContext.Consumer>
}

So basically the error message is giving you the answer.

Nothing was returned from render. This usually means a return statement is missing

withDB should return a html block. But it wasn't returning anything. Revising my code to below solved my issue.

export const withDB = Component => props => {
  return (
    <DBContext.Consumer>
        {db => <Component {...props} db={db} />}
    </DBContext.Consumer>
  )
}
Inyoung Kim 김인영
  • 1,434
  • 1
  • 17
  • 38
-1

I was getting the same error and could not figure it out. I have a functional component exported and then imported into my App component. I set up my functional component in arrow function format, and was getting the error. I put a "return" statement inside the curly braquets, "return ()" and put all my JSX inside the parens. Hopefully this is useful to someone and not redundant. It seems stackoverflow will auto format this into a single line, however, in my editor, VSCode, it's over multiple lines. Maybe not a big deal, but want to be concise.

import React from 'react';

const Layout = (props) => {
    return (
        <>
            <div>toolbar, sidedrawer, backdrop</div>
            <main>
                {props.children}
            </main>
        </>
    );
};

export default Layout;
James Mudd
  • 1,816
  • 1
  • 20
  • 25
Alex
  • 1
  • 2