Is it possible to return empty in react render function?

Xin

I have a notification component, and I have a timeout setting for it. after timeout I call this.setState({isTimeout:true}).

What I want to do is if already timeout, I want just render nothing:

  render() {
    let finalClasses = "" + (this.state.classes || "");
    if (isTimeout){
      return (); // here has some syntax error
    }
    return (<div>{this.props.children}</div>);
  }

the problem is: return (); // here has some syntax error

Mayank Shukla

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 undefined are 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' />

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is it possible to know if function uses empty return statement?

From Java

Is it possible to use if...else... statement in React render function?

From Dev

react render div if loop was empty

From Dev

Function return empty dataframe

From Dev

React Render Array in Mapped Return

From Dev

Return nothing using React render

From Dev

React.js return a style property from a function and use it inside the render() function

From Dev

React.render makes empty subcomponent in jsdom

From Dev

Test for function empty return value

From Dev

If not empty or null in JS return function

From Java

React onClick function fires on render

From Java

Dynamically building a render function in React

From Java

React Context: TypeError: render is not a function

From Dev

React-router : render is not a function

From Dev

React error: TypeError: render is not a function

From Dev

Render object arrays in react function

From Dev

React: Is it possible to prevent state update after unmount, via setting this.setState to empty function?

From Dev

Is it possible to return an array in a VBA function?

From Dev

Is there any purpose to return the result of a function in an empty function?

From Dev

Return multiple elements inside React.render()

From Dev

Express render function with callback return string and not HTML

From Dev

Express render function with callback return string and not HTML

From Dev

Getting Django function to return and render HTML page

From Dev

Pass function from props to render function in react

From Dev

Is there a way to render multiple React components in the React.render() function?

From Dev

Is it possible to "return" a function from another function?

From Dev

Get value from firebase and render it to render function in react

From Dev

How to render component from a function outside the render method in React?

From Dev

Vim function expand always return empty string

Related Related

  1. 1

    Is it possible to know if function uses empty return statement?

  2. 2

    Is it possible to use if...else... statement in React render function?

  3. 3

    react render div if loop was empty

  4. 4

    Function return empty dataframe

  5. 5

    React Render Array in Mapped Return

  6. 6

    Return nothing using React render

  7. 7

    React.js return a style property from a function and use it inside the render() function

  8. 8

    React.render makes empty subcomponent in jsdom

  9. 9

    Test for function empty return value

  10. 10

    If not empty or null in JS return function

  11. 11

    React onClick function fires on render

  12. 12

    Dynamically building a render function in React

  13. 13

    React Context: TypeError: render is not a function

  14. 14

    React-router : render is not a function

  15. 15

    React error: TypeError: render is not a function

  16. 16

    Render object arrays in react function

  17. 17

    React: Is it possible to prevent state update after unmount, via setting this.setState to empty function?

  18. 18

    Is it possible to return an array in a VBA function?

  19. 19

    Is there any purpose to return the result of a function in an empty function?

  20. 20

    Return multiple elements inside React.render()

  21. 21

    Express render function with callback return string and not HTML

  22. 22

    Express render function with callback return string and not HTML

  23. 23

    Getting Django function to return and render HTML page

  24. 24

    Pass function from props to render function in react

  25. 25

    Is there a way to render multiple React components in the React.render() function?

  26. 26

    Is it possible to "return" a function from another function?

  27. 27

    Get value from firebase and render it to render function in react

  28. 28

    How to render component from a function outside the render method in React?

  29. 29

    Vim function expand always return empty string

HotTag

Archive