question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React development error messages interfere with Ink output

See original GitHub issue

When an error boundary’s getDerivedStateFromError handler is called, React will print out a warning to the console like so:

The above error occurred in the <Example> component:
    in Example
    in ErrorBoundary
    in App

React will try to recreate this component tree from scratch using the error boundary you provided.

Because of this log output, it means that when the error boundary component is rendered after the previous component, instead of overwriting it.

I put together a small example that hopefully explains this better:

// https://github.com/colinking/ink-testing/blob/master/src/error-boundaries.tsx
import React, { useState } from 'react'
import { render, Box, Color, useInput } from 'ink'

/**
 * See: https://github.com/vadimdemedes/ink/issues/234
 * 
 * Note: the output of this script depends on your terminal width. If you have enough space,
 * the ErrorBoundary (An error occurred: Whoops!) will overwrite the "React will try to..."
 * line entirely.
 * 
 * Output:
 *  Hit any key to crash!
 *  The above error occurred in the <Example> component:
 *      in Example
 *      in ErrorBoundary
 *      in App
 *  
 *  React will try to recreate this component tree from scratch using the error boundary you proAn error occurred: Whoops!
 * Expected:
 *  An error occurred: Whoops!
 */

interface ErrorBoundaryState {
  error?: Error
}

class ErrorBoundary extends React.Component<{}, ErrorBoundaryState> {
  state = {} as ErrorBoundaryState

  static getDerivedStateFromError(error: Error): Partial<ErrorBoundaryState> {
    return { error }
  }

  render() {
    const { error } = this.state

    if (!error) {
      return this.props.children
    }

    return (
      <Box>
        <Color>An error occurred: {error.message}</Color>
      </Box>
    )
  }
}

const Example: React.FC = () => {
  const [shouldError, setShouldError] = useState(false)
  useInput(() => {
    setShouldError(true)
  })

  if (shouldError) {
    throw new Error('Whoops!')
  }

  return (
    <Box>
      <Color green>Hit any key to crash!</Color>
    </Box>
  )
}

async function run() {
  const { waitUntilExit } = render((
    <ErrorBoundary>
      <Example />
    </ErrorBoundary>
  ), {
    debug: process.env.DEBUG === 'true',
  })
  await waitUntilExit()
}

run()

You can run this like so:

$ git checkout https://github.com/colinking/ink-testing.git
$ npm i
$ npx ts-node src/error-boundaries.tsx

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
vadimdemedescommented, Sep 29, 2019

The reason it happens is because that The above error occurred in the <Example> component: is automatically written to stdout by React, it’s not rendered as part of your Ink app. These warnings occur only when NODE_ENV is not set to production (similar to client-side apps).

So if you run your app with NODE_ENV=production, this message will go away:

$ NODE_ENV=production npx ts-node src/error-boundaries.tsx
1reaction
vadimdemedescommented, Jul 24, 2022

@scalvert These are docs for Create React App, which I don’t think is relevant to React itself.

Try setting process.env.NODE_ENV before importing react.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error Boundaries - React
Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them.
Read more >
8 common React error messages and how to address them
Learn about the most common error messages in React Development and the meaning behind them, the error itself, and how to fix it....
Read more >
React + Ink CLI Tutorial – How to Build a Browser Command ...
Ink is a JavaScript library that brings React to the command line. It helps develop CLI apps using the concept of component-based UI ......
Read more >
Interactive Terminal Apps with Ink 3 - New Built-In Hooks ...
Ink 3 also intercepts React's error messages by means of a global React error boundary. Ink 3 pretty-prints the intercepted error messages and ......
Read more >
React Error Handling and Logging Best Practices
However, if any code segment throws an error, you must handle it ... and I hope it will help you develop more robust...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found