Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State changes are not accessible #42

Closed
vinyll opened this issue Mar 7, 2023 · 3 comments
Closed

State changes are not accessible #42

vinyll opened this issue Mar 7, 2023 · 3 comments
Labels
bug Something isn't working v2 Lego V2

Comments

@vinyll
Copy link
Member

vinyll commented Mar 7, 2023

The following example will toggle only once:

<script>
  const state = { enabled: false }
  
  function toggle() {
    render({ state: !state.enabled })
  }
</script>

<template>
  <p>Status is ${ state.enabled ? 'enabled' : 'disabled' }</p>
  <button @click="toggle">toggle status</button>
</template>

The reason is that const state is sent to this.state in the component init().
However the toggle() function will always refer to the state that was declared, not from the copy of state from within the component.

A solution is to refer this.state from the component to the const state by reference (default in JS).
However this has as drawback: all instances of the component will share the same state.

  • State by reference: this.state = state
  • State by copy: this.state = Object.assign({}, state)
@vinyll vinyll added bug Something isn't working v2 Lego V2 labels Mar 7, 2023
@mlbiche
Copy link

mlbiche commented Mar 9, 2023

Not sure to fully understand the issue, but if it ends to a this. use as with the extend syntax, then I would prefer the extend syntax as it is explicit that the this. refers to the class extending the Lego class. Less magic, so easier to understand.

Maybe I am misunderstanding... 🤔

@vinyll
Copy link
Member Author

vinyll commented Mar 9, 2023

It's not magic but binding.
When creating the element, function are bound to the object. Code is explicit here as a demo component: https://github.com/Polight/lego-demo/blob/main/dist/hello-world.js#L26

The same example as above would be written this way:

<script>
  const state = { enabled: false }
  
  function toggle() {
    this.render({ state: !this.state.enabled })
  }
</script>

<template>
  <p>Status is ${ state.enabled ? 'enabled' : 'disabled' }</p>
  <button @click="toggle">toggle status</button>
</template>

I still feel it's less verbose than the previous syntax. WDYT?

@vinyll
Copy link
Member Author

vinyll commented Oct 7, 2023

A new writing is fixing this issue:

<script>
  const state = { enabled: false }
  
  const methods = {
    toggle() {
      this.render({ enabled: !this.state.enabled })
    }
  }
</script>

<template>
  <p>Status is ${ state.enabled ? 'enabled' : 'disabled' }</p>
  <button @click="toggle">toggle status</button>
</template>

@vinyll vinyll closed this as completed Oct 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working v2 Lego V2
Projects
None yet
Development

No branches or pull requests

2 participants