Skip to content

Memoized child of Suspense component doesn't update when Context updates. #17356

Closed
@jcarroll2007

Description

@jcarroll2007

Do you want to request a feature or report a bug?
bug (I think?)

What is the current behavior?

const [value, setValue] = useState("default");
return (
  <div className="App">
    <input value={value} onChange={e => setValue(e.target.value)} />
    <div>
      <Value.Provider value={value}>
        <Suspense fallback={<div>loading</div>}>
          <MemoizedChild />
         </Suspense>
       </Value.Provider>
     </div>
  </div>
)

When using a memoized functional component (MemoizedChild in above example) in conjunction with Context as a child of a React.Suspense component, there seems to be a bug in which MemoizedChild does not update when the context it uses changes. For the full example, see my codesandbox below.

In the codesandbox, if you change the value of the input, the new value is provided to the context which causes the hook used in MemoizedChild (useValue) to throw a promise. This flips Suspense to the fallback state and when the promise resolves MemoizedChild's state is not updated with the proper context value because (I'm assuming) the memoized value of MemoizedChild is the one that contained the previous context value and technically no props have changed, so that makes sense why it wouldn't have updated. However, this seems like it would be unexpected behavior.

https://codesandbox.io/s/react-suspense-maybe-bug-sznbk

What is the expected behavior?
I would expect that MemoizedChild would be re-rendered with the new provided value.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
I'm assuming all of them that contain Suspense and memo. So, since 16.8?

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