Skip to content

Commit

Permalink
Show redbox in DEV on string <View> child for web
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Apr 3, 2024
1 parent 295a094 commit 5dbea46
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/platform/polyfills.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,24 @@ findLast.shim()

// @ts-ignore whatever typescript wants to complain about here, I dont care about -prf
window.setImmediate = (cb: () => void) => setTimeout(cb, 0)

if (process.env.NODE_ENV !== 'production') {
// In development, react-native-web's <View> tries to validate that
// text is wrapped into <Text>. It doesn't catch all cases but is useful.
// Unfortunately, it only does that via console.error so it's easy to miss.
// This is a hack to get it showing as a redbox on the web so we catch it early.
const realConsoleError = console.error
const thrownErrors = new WeakSet()
console.error = function consoleErrorWrapper(msgOrError) {
if (
typeof msgOrError === 'string' &&
msgOrError.startsWith('Unexpected text node')
) {
const err = new Error(msgOrError)
thrownErrors.add(err)
throw err
} else if (!thrownErrors.has(msgOrError)) {
return realConsoleError.apply(this, arguments as any)
}
}
}

0 comments on commit 5dbea46

Please sign in to comment.