Skip to content

Throwing an error from a component while server rendering changes the default value of a context if there is a provider in that tree. #14502

Closed
@emmatown

Description

@emmatown

Do you want to request a feature or report a bug?
Report a bug
What is the current behavior?
Throwing an error from a component while server rendering changes the default value of a context if there is a provider in that tree.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

https://codesandbox.io/s/03o0pnor6w

import React from "react";
import { renderToString } from "react-dom/server";

let Theme = React.createContext("light");

let Throw = () => {
  throw new Error("some error");
};

try {
  renderToString(
    <Theme.Provider value="dark">
      <Throw />
    </Theme.Provider>
  );
} catch (e) {}

// this should log "light" since it's the default value and
// there's no provider in the tree but it logs "dark" instead
console.log(renderToString(<Theme.Consumer>{x => x}</Theme.Consumer>));

What is the expected behavior?

Throwing an error from a component while server rendering shouldn't change the default value of context.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

I think this is broken in all versions of React with new context. (Here's a codesandbox of the issue with React 16.3.0, https://codesandbox.io/s/l945mq008l) It also only happens while server rendering, it works correctly in the browser (https://codesandbox.io/s/mm10ov8688)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions