← All articles

Console log with implicitly returned React components

Sometimes it can be annoying when you are trying to quickly console log something within a component that it implicitily returned, here is a quick trick.

Josh Hamilton3 min read
Editorial illustration of a glass prism revealing one block for inspection.
Reading appearance

Preferences stay in this browser.

Keep or share this article
Download Markdown

Use your browser’s print dialog to print or save a PDF.

You can also select and copy the citation above.

Look up a technical term →

Sometimes it can be annoying when you are trying to quickly console log something within a component that it implicitily returned, here is a quick trick.

Let’s take the below code for example. If you wanted to console.log(task) your first thought would be okay well now I need to change this to an explicit return so that I can log before the return right?!?!

import React from 'react'

const Task = ({ task }) => <div>The id of the task is {task.id}</div>

⬇ Like below ⬇

import React from 'react'

const Task = ({ task }) => {
  console.log(task)

  return <div>The id of the task is {task.id}</div>
}

However there is a much easier way of doing this that won’t make you rewrite how the component returns the JSX.

The thing to think about in the below syntax is what does console.log(task) evaluate to? false right, and because of this we can || the result

import React from 'react'

const Task = ({ task }) =>
  console.log(task) || <div>The id of the task is {task.id}</div>

Simple enough right?

Your private notes

Only on this device. Notes are never published or sent to a server. Keep up to 100 notes, with 10,000 characters per article. A full store never discards an older note.

Enable JavaScript to keep private notes.

Keep the curiosity going

ai

When AI in an interview fails you

I used AI heavily in a technical interview, felt great about the build, and still got rejected because the team could not gauge enough of my understanding.

6 min read
react

Getting started with React and ViteJS

In this blog post I go over the basic scaffolding of building out a starter React app using ViteJS and explain ViteJS works compared to something like your traditional module loaders such as Webpack.

6 min read
ai

Building AGFS.dev on my wife's MacBook Neo with an AI agent

Why the citrus MacBook Neo is more capable than it looks, and why AI-assisted coding makes it a legit first developer machine.

6 min read

Article image

Open original image ↗

Keyboard shortcuts

Shortcuts stay out of text fields. Extra shortcuts are off until you enable them on this device. Ctrl or Command K is always available.

?
Open this help
Ctrl / ⌘ K
Open site search
Alt A
Browse articles
Alt R
Open your reading list
Alt T
Browse topics
Escape
Close dialogs or leave article focus mode