Skip to content

Not sure why new state isn't reflected in html when using setState and Template Literals? #325

Answered by ryansolid
rencire asked this question in Q&A
Discussion options

You must be logged in to vote

Yeah this is why I strongly recommend JSX. State proxies track on property access.. ie state.count.. it basically internally calls a function. In the signal examples you are passing that function in and the view code executes it, but in the state code it is being executed first, which doesn't track. The solution, wrap it with a function.

 // Ex. 4
  // const [state, setState] = createState({count: 0});
  // const handler = () => {
  //   console.log(state.count);
  //   setState({count: state.count+1});
  // };
  // return html`
  //   <div>
  //     <p>${() => state.count}</p>
  //     <button onclick=${handler}>click me</button>
  //   </div>
  // `;

You can also see this example: https…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@rencire
Comment options

Answer selected by rencire
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants