Git Product home page Git Product logo

Comments (16)

ArnaudBarre avatar ArnaudBarre commented on June 12, 2024 9

Ok just tested the repro and I confirm this is fixing the issue.

Sadly the PR is blocked by a change in esbuild that is shipped with Vite 4. This was fixed two weeks ago and will be included in Vite next minor (currently in beta, so hopefully in less than two weeks).

I will then publish v3.1 with this fix and require Vite 4.1 as a peer dependency.

from vite-plugin-react.

sapphi-red avatar sapphi-red commented on June 12, 2024 6

I confirmed this still does not work. (Vite 4.0.2 + plugin-react 3.0.0)
TestMyHoc.jsx now works but Test.jsx still doesn't work.
https://stackblitz.com/edit/vitejs-vite-gdqpfv?file=package.json,src%2FTestMyHoc.jsx,src%2FTest.jsx

from vite-plugin-react.

gaearon avatar gaearon commented on June 12, 2024 4

The Fast Refresh (from react/refresh) only re-run the variable Component (the first capitalized),

That's expected. React component names must always start with a capital letter. This convention is assumed by some other tooling as well so you need to follow it.

from vite-plugin-react.

danieloprado avatar danieloprado commented on June 12, 2024 4

Here: https://stackblitz.com/edit/vitejs-vite-k3bzhu?file=src/Test.jsx

Try to change the background-color or try to change the parameter of the TestMyHoc. TestMyHocWorking is an example where refresh is working as expected, I think is because is not a call assignment.

from vite-plugin-react.

crtl avatar crtl commented on June 12, 2024 1

I am using a helper function to create and wrap styled function components and it worked using create-react-app but using vite I have to reload for every change.

import {FC} from "react";
import styled from "styled-components";


/**
 * Creates a function component and wraps it in styled-component to enable styling
 * and generates a new displayName for returned component if provided component has one.
 * Usage:
 * ```
 * const MyComponent = createStyledFC<{}>((props) => {
 *     return <div {...props}></div>;
 * })`
 *   background: blue;
 *
 *   .my-styled {
 *       color: red;
 *   }
 * `;
 * ```
 * @param component
 * @param displayName Optional display name for component
 */
export function createStyledFC<T = {}>(component: FC<T & {className?: string}>, displayName?: string) {

    const comp = styled(component as FC<T & {className?: string}>)

    const _displayName = displayName || component.displayName;

    if (_displayName) {
        component.displayName = _displayName;
        (comp as any).displayName = `Styled${_displayName}`;
    }

    return comp;
}

from vite-plugin-react.

ArnaudBarre avatar ArnaudBarre commented on June 12, 2024 1

It could be fixed by #79
This is using a more precise invalidation algorithm already used in the SWC plugin

from vite-plugin-react.

danieloprado avatar danieloprado commented on June 12, 2024

Based on this comment I discovery a workaround:

const Component = styled(({ className }) => { // <~ wrap the component here, not in export
 return <div className={className}>hi!</div>
})`
    background: red;
`

This is not a good workaround because it looses the syntax highlighting but it may give some idea how to fix this problem.

from vite-plugin-react.

danieloprado avatar danieloprado commented on June 12, 2024

The real problem is not with styled, is with any HOC.

const Component = ({ className }) => {
 return <div className={className}>hi!</div>
}

const myHOC = (Component, className) => {
 return  (props) => <Component {...props} className={className} />
}

export default myHoc(Component, 'test-class-1') // <~ try to change, no effect.

The Fast Refresh (from react/refresh) only re-run the variable Component (the first capitalized),
if I change the capitalized letters, it worked!

const component = ({ className }) => {
 return <div className={className}>hi!</div>
}

const MyHOC = (Component, className) => {
 return  (props) => <Component {...props} className={className} />
}

export default MyHoc(Component, 'test-class-1') // <~ try to change, no effect.

I don't know if there is any fix for that. 😥

Example:

example.mp4

from vite-plugin-react.

danieloprado avatar danieloprado commented on June 12, 2024

Maybe related to facebook/react#20417 and facebook/react#21104

from vite-plugin-react.

danieloprado avatar danieloprado commented on June 12, 2024

@patak-js please reopen we still discussing it haha
@gaearon it was the workaround that I found to try to understand the behaviour.

The problem is with this very common syntax:

const Component =({ className }) => {
 return <div className={className}>hi!</div>
}

export default styled(Component)`
    background-color: red;
`

When you try to change the background-color it does not refresh current value. But when you change the content inside the component (like adding a text), the text is added but the background still the same as before. The fast refresh only refreshes the Component not the styled (or any HOC values)

from vite-plugin-react.

patak-dev avatar patak-dev commented on June 12, 2024

Does this work with Webpack and not with Vite @danieloprado? Could you create a minimal reproduction that showcases the issue in Vite? Maybe using StackBlitz https://vite.new/react?

from vite-plugin-react.

danieloprado avatar danieloprado commented on June 12, 2024

I've just update the code with more examples.

from vite-plugin-react.

danieloprado avatar danieloprado commented on June 12, 2024

Here an example with rect-sctipts: https://stackblitz.com/edit/react-ptscnt?file=src%2FTest.jsx

from vite-plugin-react.

oyb81076 avatar oyb81076 commented on June 12, 2024
// Button.tsx
export default function Button(): React.ReactElement | null {
  return (
     <SButton>Text</SButton>
  );
}
const SButton = styled.button`color: green;`;
export const ButtonGroups = styled.button`
color: blue;
`;

当Button和ButtonGroups在一个文件里面的时候,
修改SButton的样式热更新正常,修改ButtonGroups的样式,不会触发热更新.
如果把ButtonGroups 和 Button 拆分到两个文件中,一切都很正常

from vite-plugin-react.

dominikfucic avatar dominikfucic commented on June 12, 2024

I was able to reproduce bug.

Create .tsx file and inside create a styled-component with export, and a functional component with default export.

Import both components inside App.tsx, wrap the styled-component with the functional component.

Try changing styles on styled-component.

HMR is not working.

from vite-plugin-react.

ZombieChowder11 avatar ZombieChowder11 commented on June 12, 2024

Recently ran into this issue myself and I was wondering if there have been any official updates on this?

from vite-plugin-react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.