-
-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React Hot Reload - Styled Component/Emotion #6
Comments
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. |
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), 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 |
Maybe related to facebook/react#20417 and facebook/react#21104 |
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. |
@patak-js please reopen we still discussing it haha 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) |
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? |
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. |
I've just update the code with more examples. |
Here an example with rect-sctipts: https://stackblitz.com/edit/react-ptscnt?file=src%2FTest.jsx |
// 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在一个文件里面的时候, |
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;
}
|
I confirmed this still does not work. (Vite 4.0.2 + plugin-react 3.0.0) |
Describe the bug
Hot reload won't update the css created by styled, just after a full reload.
Reproduction
Just try to change any css's prop inside a styled:
https://github.com/eduzz/template-react/tree/vite
System Info
Used Package Manager
yarn
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: