Code Monkey home page Code Monkey logo

Comments (8)

EverettMcKay avatar EverettMcKay commented on July 17, 2024 1

Ok, that fixed it! Thank you!

Might seem like a dumb thing to not have tried onbeforeupdate, but I used the debugger to check if the state was correct on redraw. oninit was getting called and the state was getting updated correctly so I assumed the problem must be elsewhere.

from mithril.js.

EverettMcKay avatar EverettMcKay commented on July 17, 2024

Sorry, the problem is worse than I thought. I removed a bit of complexity from the snippet above. The code is really more like:

  view: (v: Vnode<myAttrs, myState>) => (
  <>
      <Title {...v.attrs} />
      <div style="margin:8px 0;display:inline-block;">
        {v.state.range.map((info, i) => (
          <button
            style={myStyle}
            onclick={() => {
              handleClick(); }}
          >
            {getButtonLabel(v.attrs, v.state)}
          </button>
        ))}
      </div>
  </>
  ),

Just discovered that even though v.state is updating as expected, <Title ...>, which is based on v.state, doesn't redraw either. But for whatever reason, but button labels do redraw which is why I was confused.

So, it's not a key problem.

I guess my real question is: how do I force a component to redraw itself when it's state changes? Most of the time everything works as expected, but sometimes there appears to be nothing you can do to convince a component to redraw on state change.

Using a key on a parent node seems to work, but in this case using a key is impractical. (I seem to get an endless supply of "use a key on one, use a key on all" errors.)

from mithril.js.

cavemansspa avatar cavemansspa commented on July 17, 2024

are you using m.render() (i.e. m.render() does not autoredraw)? see autoredraw, which is applied when using am.mount() or m.route().

from mithril.js.

EverettMcKay avatar EverettMcKay commented on July 17, 2024

I'm using m.route(). As mentioned, in all other places autoredraw is working as expected.

from mithril.js.

osban avatar osban commented on July 17, 2024

It looks like in Title you're only deconstructing v.attrs, not v.state?

from mithril.js.

EverettMcKay avatar EverettMcKay commented on July 17, 2024

Correct. v.state is updated to reflect the current v.attrs, which happens in oninit.

from mithril.js.

osban avatar osban commented on July 17, 2024

Ok, not sure I understand completely how things are done, but oninit only runs once when the component is created, not when redraws happen. If you'd want it to happen before every (re)draw of the view, use onbeforeupdate.

from mithril.js.

EverettMcKay avatar EverettMcKay commented on July 17, 2024

Closing. Thanks again!

from mithril.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.