(client) ss="user-button sc-bdVaJa hJPaNh" data-r (server) ss="user-button sc-bwzfXH eerwGk" data-r
See original GitHub issuedescribe:
server side render throw below error when use extend
:
styleComponentCode:
// button component
import React from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
// 暴露行为 onClick
class Button extends React.Component {
constructor(props) {
super(props);
}
click() {
this.props.onClick();
}
render() {
const {
className,
onClick,
children
} = this.props;
return (
<div className={className} onClick={() => onClick()}>
{children}
</div>
);
}
}
// button style component
export const DefaultButton = styled(Button)`
font-size:14px;
background-color: ${props => (props.disable ? '#d7d7d7' : '#f60')};
padding: 8px 0;
border: 0;
width: ${props => (props.size ? Size[props.size] : '240px')};
text-align: center;
color: #fff;
border-radius: 4px;
&:hover{
${
props => defauHoverFunc(props)
}
}
`;
// use extend to extranl button
export const DottedButton = DefaultButton.extend`
background-color: inherit;
border: 1px solid ${props => (props.disable ? '#d7d7d7' : '#f60')};
color: ${props => (props.disable ? '#d7d7d7' : '#f60')};
&:hover{
${
props => dotHoverFunc(props)
}
}
`;
run env:
“styled-components”: “^2.1.0” “next”: “latest”, “react”: “15.6.1”, “react-dom”: “15.6.1”,
Issue Analytics
- State:
- Created 6 years ago
- Comments:17 (13 by maintainers)
Top Results From Across the Web
No results found
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have same issue and I realized that error occurs in case of re-requiring compiled entrypoint (
src/serverRender.jsx
, for example).In production environment
src/serverRender.jsx
is required once (on boot) inindex.js
. But in dev environment it’s necessary re-requiresrc/serverRender.jsx
after each modification in sources.I prepared demo application to reproduce issue: https://github.com/braska/styled-components-ssr-issue-demo
I have tried to add
console.log(identifiers);
to 73 line innode_modules/styled-components/lib/models/StyledComponent.js
(https://github.com/styled-components/styled-components/blob/master/src/models/StyledComponent.js#L33) to determinate why server side environment generate wrong class names. I got unexpected results. See server console output:Problem is that
app__Button
field contains2
now. But in browser this field always contains1
.For example, the initial HTML has this comment in the generated styles:
but React complains that
How can I troubleshoot the rehydration?
Note that I’m running in hot reload mode on server and client, but doing SSR. Somehow this must be related, because when I run the production build, I don’t get the error (but not sure if React outputs the error then)