Styled components css produces "Uncaught Error" with useState hook
See original GitHub issueWhen using styled-components and a useState hook, an Uncaught Error: Rendered fewer hooks than expected. This may be caused by an accidental early return statement.
is produced when trying to set the state.
Example code that should give error:
import kotlinx.css.Color
import kotlinx.css.backgroundColor
import kotlinx.html.js.onClickFunction
import react.RBuilder
import react.RProps
import react.child
import react.dom.button
import react.dom.p
import react.dom.render
import react.functionalComponent
import react.useState
import styled.css
import styled.styledDiv
import kotlin.browser.document
fun main() {
render(document.getElementById("root")) {
exampleStyledComponent()
}
}
val exampleStyledComponent = functionalComponent<RProps> {
val (count, setCount) = useState(1)
styledDiv {
css {
backgroundColor = Color.red
}
button {
attrs {
onClickFunction = {
setCount(count + 1)
}
}
+"Increment"
}
p { +"$count" }
}
}
fun RBuilder.exampleStyledComponent() = child(exampleStyledComponent)
In this example, if I remove the backgroundColor = Color.red
from the css lambda, setCount
works.
I am using kotlin js 1.3.70-eap-274.
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Using useState-hook to alter styled-components from within
Lifting the local state up makes the component not self-contained, it will have to rely on the parent to function properly. It's also...
Read more >React Hooks Common Mistakes - Bugfender
As you've hopefully spotted, the code reaches Uncaught RangeError: Maximum call stack size exceeded and cause an infinity loop. This happens ...
Read more >A Guide to Common React Errors
Error: React Hook "useState" is called conditionally. React Hooks must be called in the exact same order in every component render. function App() ......
Read more >Invalid Hook Call Warning - React
You are probably here because you got the following error message: Hooks can only be called inside the body of a function component....
Read more >Using Styled Components - Expo Documentation
Using Styled Components. Styled Components is a CSS-in-JS solution that enables you to create React components with a given style very easily. Using...
Read more >
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 Free
Top 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
I am getting this exact behaviour. My dependencies:
EDIT: This issue seems to be caused by using a newer version of styled-components. fixed by specifying:
We are hoping to release
kotlin-styled-next@1.0
really soon at which point the disclaimer in the documentation will be removed.