question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Styled components css produces "Uncaught Error" with useState hook

See original GitHub issue

When 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:closed
  • Created 4 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
pillimojcommented, Apr 2, 2020

I am getting this exact behaviour. My dependencies:

dependencies {
    implementation(kotlin("stdlib-js"))
    implementation("org.jetbrains:kotlin-react:16.13.0-pre.94-kotlin-1.3.70")
    implementation("org.jetbrains:kotlin-react-dom:16.13.0-pre.94-kotlin-1.3.70")
    implementation(npm("react", "16.13.0"))
    implementation(npm("react-dom", "16.13.0"))

    implementation("org.jetbrains:kotlin-styled:1.0.0-pre.94-kotlin-1.3.70")
    implementation(npm("styled-components"))
    implementation(npm("inline-style-prefixer"))

    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.3")
}

EDIT: This issue seems to be caused by using a newer version of styled-components. fixed by specifying:

implementation(npm("styled-components", "4.4.0"))
1reaction
Leonyacommented, Oct 29, 2021

We are hoping to release kotlin-styled-next@1.0 really soon at which point the disclaimer in the documentation will be removed.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found