Comments (16)
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.
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.
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.
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.
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.
It could be fixed by #79
This is using a more precise invalidation algorithm already used in the SWC plugin
from vite-plugin-react.
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.
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.
Maybe related to facebook/react#20417 and facebook/react#21104
from vite-plugin-react.
@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.
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.
I've just update the code with more examples.
from vite-plugin-react.
Here an example with rect-sctipts: https://stackblitz.com/edit/react-ptscnt?file=src%2FTest.jsx
from vite-plugin-react.
// 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.
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.
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)
- HMR improvements for .ts files that include jsx
- Default ESLint setup ignores JSX files if you run `npx eslint .` HOT 3
- Injecting Preamble not possible when using `renderToPipeableStream`. HOT 2
- inject css in shadowDom HOT 1
- Does the React plugin not support React 16? I couldn't find related documentation. HOT 1
- react-beautiful-dnd拖拽插件在vite+react上面不能使用,react的脚手架就没有问题
- react-beautiful-dnd在vite脚手架下面不能拖拽
- **Update** It worked when I allocated a whopping 16GB of RAM for Vite's initialization cycle.
- Conflict with plugin-vue-jsx HOT 2
- Can not support hmr x include files inside node_modules
- vercel build, Writable' is not exported by __vite-browser-external HOT 1
- gettting jsxDev is not defined error while using LexicalRichTextPlugin in react HOT 1
- Could not resolve !!raw-loader!prismjs HOT 4
- Heading dropdown is not working for font size change like Heading 1 Heading 2
- ESLint upgrade needed HOT 1
- ESBuild supports decorators now HOT 1
- Running React Compiler not successful HOT 2
- Vite HMR not work with framer motion in dev mode HOT 2
- supporting nextjs
- The 4.3.0 version break React compiler setup HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vite-plugin-react.