React development error messages interfere with Ink output
See original GitHub issueWhen 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:
- Created 4 years ago
- Comments:8 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 whenNODE_ENV
is not set toproduction
(similar to client-side apps).So if you run your app with
NODE_ENV=production
, this message will go away:@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 importingreact
.