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

Example code not working #333

Closed
thegregthomp opened this issue Mar 29, 2023 · 8 comments
Closed

Example code not working #333

thegregthomp opened this issue Mar 29, 2023 · 8 comments

Comments

@thegregthomp
Copy link

thegregthomp commented Mar 29, 2023

I couldn't get rerenders working on a test scenario I created so I re-read the docs and found the signals demo. That also doesn't appear to work/re-render. Anyone know what's going on? https://preactjs.com/repl?example=todo-list-signals

@thegregthomp
Copy link
Author

Further exploration leads me to see that using the signal variable it's self works and updates but using signal.value doesn't update. See example below:

Works:

const counter = signal(1);

//In component
{counter}

Doesn't work:

const counter = signal(1);

//In component
{counter.value}

in the above use-case, this is fine but for objects I don't really see a way around not using value and thus why I think the example is failing.

@rschristian
Copy link
Member

For the REPL not working, see: preactjs/preact-www#978. It's a recent bug in the REPL, it'll (hopefully) get fixed shortly.

Are you using React or Preact? Might be a duplicate of #298 if the former.

@thegregthomp
Copy link
Author

thegregthomp commented Mar 29, 2023

@rschristian thanks for the response. I am using react and I hadn't seen that bug previously and made the changes to test. I still can not get a signal with an object to re-render. In the below example the incrementation will happen but wont show until I cause a rerender by other means.

const complexSignal = signal({ a: 1, b: 2, c: {d: 3, e:4, f: {g:5}} });

export default function Counter() {
  
  return (
    <div>
      <h1>Counter</h1>
      <button onClick={()=>complexSignal.value.a++}> UPDATE A: {complexSignal.value.a}</button>
    </div>
  );
}

@thegregthomp
Copy link
Author

thegregthomp commented Mar 29, 2023

It may be of value to know that this is a vite project using the react-swc-ts template. I'm noticing issues with HMR and signals (which is a know issue here), not sure if it's related.

@thegregthomp
Copy link
Author

Here's an update example (https://codesandbox.io/s/react-playground-forked-8d9yi5?file=/index.js) if you increment counter it works. If you increment the complexSignal it works without re-render. Then go increment counter again and you'll see on re-render the incremented complexSignal is correct

@Kanaye
Copy link

Kanaye commented Mar 29, 2023

In your example you aren't updating the signal, but a property of the object of the signal.
This means the signal doesn't "know" that an update occurred and therefore can't trigger an update.

You will also need to make sure that your newValue != oldValue, otherwise signals won't trigger an update.

I updated your example to work with these limitations: https://codesandbox.io/s/react-playground-forked-hmbk8t?file=/index.js

@thegregthomp
Copy link
Author

@Kanaye thanks so much. I just found this chain of comments as well that help: #120 (comment)

@rschristian
Copy link
Member

Going to close this out then, you can subscribe to the linked issues to get updates on them.

Feel free to ping if you run into any other issues.

@rschristian rschristian closed this as not planned Won't fix, can't repro, duplicate, stale Mar 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants