I have a stateless functional component in React 0.14 that worked in React 0.13, but now returns the following error:
No
render
method found on the returned component instance: you may have forgotten to definerender
, returned null/false from a stateless component, or tried to render an element whose type is a function that isn't a React component.
This is my component:
function ToggleDisplay(props) { //should render a <noscript> per React's implementation if(props.if === false) { // return <noscript></noscript>; //do I have to do this manually now? return null; } let style = {}; if(shouldHide(props)) { style.display = 'none'; } return ( <span style={style} {...props} /> ); }
Do I have to manually return a <noscript>
now? Is there another way to return null in stateless component?
When null is returned from a React component, nothing gets rendered. Copied! The condition in the example checks for the value of the count state variable and if it's greater than 3 , it returns null . If you return null from a component, nothing is rendered.
A stateless function component is a typical React component that is defined as a function that does not manage any state. There are no constructors needed, no classes to initialize, and no lifecycle hooks to worry about. These functions simply take props as an input and return JSX as an output.
Initialization that requires DOM nodes should go here. If you need to load data from a remote endpoint, this is a good place to instantiate the network request. As I understand it correctly: component returns null , so that there's nothing to mount in the DOM tree and componentDidMount should not be fired.
A functional component is just a plain JavaScript pure function that accepts props as an argument and returns a React element(JSX). A class component requires you to extend from React. Component and create a render function which returns a React element. There is no render method used in functional components.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With