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;
the problem is in the return, try this:
this solved my problem
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.
Got the answer: I should not use parentheses after
return ()
. This works:If you want to write multiline, then
return ( ...
Your starting parenthesis should be on the same line as
return
.We had a component enclosed in the curly braces i.e.
{
and}
:Replacing them with the round brackets i.e.
(
and)
fixed the issue:This error can be seen for a number of reasons:
Error:
Correct way to implement
render
:In the above example, the semicolon at the end of the
return
statement will not make any difference.Error:
Correct way:
In the error example, we have used curly braces but we have to use round brackets instead. This also gives the same error.
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
instead of
using the latter solved the issue.