DEV Community

devterminal
devterminal

Posted on

 

Self-invoking function as React children

I always wanted to be able to use return statement on children rendering. Self-Invoking Function is the solution. This is an example of building page states inside of the Layout component. πŸ’‘πŸ§‘β€πŸ’»

const SelfInvokingChildren = () => {
  const [loading, error, data] = useData()

  return (
    <Layout>
      {(function () {
        if (loading) return <Placeholder />

        if (error) return <Error />

        if (!data) return <NotFound />

        return <PageContent data={data} />
      })()}
    </Layout>
  )
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.