Yes you can, but instead of blank, simply return null if you don't want to render anything from component, like this:
return (null);
Another important point is, inside JSX if you are rendering element conditionally, then in case of condition=false, you can return any of these values false, null, undefined, true. As per DOC:
booleans (true/false), null, and undefinedare valid children, they will be Ignored means they simply don’t render.
All these JSX expressions will render to the same thing:
<div />
<div></div>
<div>{false}</div>
<div>{null}</div>
<div>{undefined}</div>
<div>{true}</div>
Example:
Only odd values will get rendered, because for even values we are returning null.
const App = ({ number }) => {
if(number%2) {
return (
<div>
Number: {number}
</div>
)
}
return (null); //===> notice here, returning null for even values
}
const data = [1,2,3,4,5,6];
ReactDOM.render(
<div>
{data.map(el => <App key={el} number={el} />)}
</div>,
document.getElementById('app')
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='app' />
Answer from Mayank Shukla on Stack OverflowYes you can, but instead of blank, simply return null if you don't want to render anything from component, like this:
return (null);
Another important point is, inside JSX if you are rendering element conditionally, then in case of condition=false, you can return any of these values false, null, undefined, true. As per DOC:
booleans (true/false), null, and undefinedare valid children, they will be Ignored means they simply don’t render.
All these JSX expressions will render to the same thing:
<div />
<div></div>
<div>{false}</div>
<div>{null}</div>
<div>{undefined}</div>
<div>{true}</div>
Example:
Only odd values will get rendered, because for even values we are returning null.
const App = ({ number }) => {
if(number%2) {
return (
<div>
Number: {number}
</div>
)
}
return (null); //===> notice here, returning null for even values
}
const data = [1,2,3,4,5,6];
ReactDOM.render(
<div>
{data.map(el => <App key={el} number={el} />)}
</div>,
document.getElementById('app')
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='app' />
Some answers are slightly incorrect and point to the wrong part of the docs:
If you want a component to render nothing, just return null, as per doc:
In rare cases you might want a component to hide itself even though it was rendered by another component. To do this return null instead of its render output.
If you try to return undefined for example, you'll get the following error:
Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.
As pointed out by other answers, null, true, false and undefined are valid children which is useful for conditional rendering inside your jsx, but it you want your component to hide / render nothing, just return null.
EDIT React 18:
React 18 will allow rendering undefined instead of throwing. See this announcement.
reactjs - Return null from a stateless component/"functional component" - Stack Overflow
reactjs - What is the best case for defining function without any dependencies in React functional component? - Stack Overflow
Same file components vs functions that return JSX
Is it possible to use React without rendering HTML?
Videos
As of React 15.0, you can return null from a stateless functional component. (See #5355). No more having to return <noscript />
The change that made this possible is that React removed support for component classes that don’t inherit from React.Component, meaning they can reliably differentiate between React components (classes that inherit React.Component) and functional stateless components. So the PR to enable the functionality just involved removing and simplifying the code that instantiates the components.
Looks like not, this is a technical constraint in Javascript. To support arrow functions and plain functions as "components" React needs to know if we can call new on them.
We can call new on everything if they're plain functions as long as they return a ReactElement. However, that won't work for null/false/string return values and we want to support those too. We also can't call new on arrow functions. Likewise, we can't NOT call new on classes.
Relevant GitHub Issue
Hey ya'll, question about the performance/best practice when comparing using a same file component that, let's say will never be re-used, vs adding a function inside the main component that simply returns some JSX.
These examples are totally simplified here, and assume each example is a single file.Let's say this:
export const App = () => {
const [title, setTitle] = useCustomHook('My Title');
const renderTitle = () => {
return <h1>{title}</h1>;
};
return (
<>
{renderTitle()}
<p>Some other text...</p>
</>
);
};
vs this:
const MyTitle = (title) => {
return <h1>{title}</h1>;
};
export const App = () => {
const [title, setTitle] = useCustomHook('My Title');
return (
<>
<MyTitle title={title} />
<p>Some other text...</p>
</>
);
};
I have always leaned towards the first option as long as I know that the JSX that's returned from the renderTitle function is only relevant to App.
Thoughts?
As of React >= 16.2 it is possible to use any of these versions:
render() {
return false;
}
render() {
return null;
}
render() {
return [];
}
render() {
return <React.Fragment></React.Fragment>;
}
render() {
return <></>;
}
Returning undefined does not work.
The component I'm thinking of is something that you pass some data to, and it'll send data back to a javascript function outside of react.
Why would you want to create a component for that? Most of the time a regular js function in an existing component can be enough.
One usecase is for exemple to setup a side-effect when component is mounted and tear it down when it unmounts. For exemple if you have a ReactNative mobile app with portrait orientation, you could imagine a <Landscape/> component, that, when mounted, would allow temporarily to display the app in landscape orientation, and when unmounted, orientation would be reset to app default. You can surely manage this orientation change on an existing component, but creating a dedicated component might be more handy and reusable.
Note that React can also run on the server side so I guess it is possible to use it in such a way that it doesn't involve any DOM modifications (but maybe only the virtual DOM computation).
Just to clarify benno's comments. The ReactComponent.render method doc states:
You can also return
nullorfalseto indicate that you don't want anything rendered. Behind the scenes, React renders a<noscript>tag to work with our current diffing algorithm. When returningnullorfalse,this.getDOMNode()will returnnull.
function Comp1() {return;}
function Comp2() {return null;}
Why does the first one throw "nothing returned from render" and second just works? Is it because of some philosophical decision at the time of the creation of React, or is there some technical reason, like maybe internal usage of some native function like Object.create in React code, which does a distinction between undefined and null?
https://beta.reactjs.org/learn/adding-interactivity#state-a-components-memory
export default function Gallery() {
const [index, setIndex] = useState(0);
const [showMore, setShowMore] = useState(false);
function handleNextClick() {
setIndex(index + 1);
}
function handleMoreClick() {
setShowMore(!showMore);
}
....So on every render we are creating new functions handleNextClick, handleMoreClick
Should we pull these out of Gallery and pass the "set" state functions as parameters?
I only ask this because I read somewhere best practice is to NOT have functions inside your React functional components <-- trying to confirm if this is true? Small functions allowed? Doesn't matter?