Skip to content

[react] HMR not working with HOC #21

Closed
@marovargovcik

Description

@marovargovcik

Describe the bug

Description

There is a very odd behavior in my project with HMR not updating what the browser is displaying after editing a component.

  • I have a page component About in src/About.tsx that is used as an element for react-router <Route> in src/App.tsx.
  • This page component About is also wrapped with HOC withRandom from src/withRandom.tsx.
  • A Component defined in the same src/About.tsx file is wrapped with HOC withCounter from src/withCounter.tsx and used as one of children of About component

This combination makes HMR not working as expected because any editing to About component is not updating what is being displayed by the browser.

Findings

Updating <h1>original text</h1> in About component does not update the text displayed in the browser.

const Component = memo(() => (
  <div style={{ border: '1px solid black', padding: '8px' }}>
    <b>This is component defined in about.tsx</b>
  </div>
));

const ComponentWithCounter = withCounter('You are finally awake.', Component);

const About = withRandom(
  'One does not simply reproduce a bug in isolated environment',
  memo(() => {
    return (
      <>
        <b>This is about component</b>
        <div style={{ border: '1px solid blue', padding: '8px' }}>
          <ComponentWithCounter>
            {({ decrement, increment }) => (
              <>
                <button onClick={increment} type='button'>
                  +
                </button>
                <button onClick={decrement} type='button'>
                  -
                </button>
              </>
            )}
          </ComponentWithCounter>
          <h1>original text</h1>
        </div>
      </>
    );
  })
);

But after removing/commenting out anything related to withCounter HOC About component reacts to changes as it should:

import { memo } from 'react';

// import { withCounter } from '@/withCounter';
import { withRandom } from '@/withRandom';

// const Component = memo(() => (
//   <div style={{ border: '1px solid black', padding: '8px' }}>
//     <b>This is component defined in about.tsx</b>
//   </div>
// ));

// const ComponentWithCounter = withCounter('You are finally awake.', Component);

const About = withRandom(
  'One does not simply reproduce a bug in isolated environment',
  memo(() => {
    return (
      <>
        <b>This is about component</b>
        <div style={{ border: '1px solid blue', padding: '8px' }}>
          {/* <ComponentWithCounter>
            {({ decrement, increment }) => (
              <>
                <button onClick={increment} type='button'>
                  +
                </button>
                <button onClick={decrement} type='button'>
                  -
                </button>
              </>
            )}
          </ComponentWithCounter> */}
          <h1>original text - changed!</h1>
        </div>
      </>
    );
  })
);

Same goes for removing/commenting out anything related to withRandom:

import { memo } from 'react';

import { withCounter } from '@/withCounter';

const Component = memo(() => (
  <div style={{ border: '1px solid black', padding: '8px' }}>
    <b>This is component defined in about.tsx</b>
  </div>
));

const ComponentWithCounter = withCounter('You are finally awake.', Component);

const About = memo(() => {
  return (
    <>
      <b>This is about component</b>
      <div style={{ border: '1px solid blue', padding: '8px' }}>
        <ComponentWithCounter>
          {({ decrement, increment }) => (
            <>
              <button onClick={increment} type='button'>
                +
              </button>
              <button onClick={decrement} type='button'>
                -
              </button>
            </>
          )}
        </ComponentWithCounter>
        <h1>original text</h1>
      </div>
    </>
  );
});

However I see changes in network tab - the browser just does not update for some reason:

Steps to reproduce

  1. Open the provided reproduction case
  2. Start vite (if it is not already running) with npm run vite:start
  3. in src/About.tsx edit text in <h1> tag of About component
  4. Browser should be updated with new text that was just edited in the previous step but it isn't

Reproduction

https://stackblitz.com/edit/node-tqfxtx?file=src%2FAbout.tsx

System Info

System:
    OS: Linux 5.10 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (16) x64 AMD Ryzen 9 5900HX with Radeon Graphics
    Memory: 1.73 GB / 7.47 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
Binaries:
    Node: 16.13.1 - ~/.nvm/versions/node/v16.13.1/bin/node
    npm: 8.2.0 - ~/.nvm/versions/node/v16.13.1/bin/npm
npmPackages:
    @vitejs/plugin-react: ^1.1.4 => 1.1.4 
    vite: ^2.7.10 => 2.7.10

Used Package Manager

npm

Logs

[vite] hot updated: /src/About.tsx

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: hmrp3-minor-bug 🔨An edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions