Skip to content

Style Cannot Be Updated #480

Closed
Closed

Description

@rmorshea

When clicking the button in the code below it's background color should change, but it does not:

import idom


@idom.component
def Test():
    color_toggle, set_color_toggle = idom.hooks.use_state(False)
    color = "red" if color_toggle else "blue"
    return idom.html.div(
        idom.html.button(
            {
                "onClick": lambda event: set_color_toggle(not color_toggle),
                "style": {"backgroundColor": color, "color": "white"},
            },
            "toggle",
        ),
    )


idom.run(Test, port=5000)

Originally reported by @jgburford in #478 (reply in thread)

Activity

  1. rmorshea commented on Aug 27, 2021

    @rmorshea
    CollaboratorAuthor

    Probably the cause of the broken snake game example: #451

  2. rmorshea commented on Aug 27, 2021

    @rmorshea
    CollaboratorAuthor

    This fixes it:

    import idom
    import random
    
    
    @idom.component
    def Test():
        color_toggle, set_color_toggle = idom.hooks.use_state(False)
        color = "red" if color_toggle else "blue"
        return idom.html.div(
            idom.html.button(
                {
                    "onClick": lambda event: set_color_toggle(not color_toggle),
                    "style": {"backgroundColor": color, "color": "white"},
                },
                "toggle",
                key=str(random.random()),
            ),
        )
    
    
    idom.run(Test, port=5000)

    There's something wrong with keys...

  3. rmorshea commented on Aug 27, 2021

    @rmorshea
    CollaboratorAuthor

    Turns out its because we started modifying the view model inplace using fast-json-patch - React was checking to see if the identity of the style changed, and since it was modified in place, it never was. This was a pretty old bug I just never caught.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions