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

Can't use Date Object in state? #19

Closed
Meigyoku-Thmn opened this issue Apr 5, 2019 · 2 comments
Closed

Can't use Date Object in state? #19

Meigyoku-Thmn opened this issue Apr 5, 2019 · 2 comments
Labels
bug Something isn't working

Comments

@Meigyoku-Thmn
Copy link

Meigyoku-Thmn commented Apr 5, 2019

Hello, for some reason this simple code (I use your Counter example) throw error when I'm trying to use Date Object method from Solidjs state:

<html>
  <head>
    <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
    <script src="https://unpkg.com/solid-standalone"></script>
    <script type='text/solid'>
      const { createRoot, createState, onCleanup } = Solid;
      
      const App = () => {
        const [state, setState] = createState({ counter: 0, dateTime: new Date() }),
          timer = setInterval(() => setState({ counter: state.counter + 1 }), 1000);
        onCleanup(() => clearInterval(timer));
        return <div>{( state.counter.toString() + ' ' + state.dateTime.toString() )}</div>
      }
      
      createRoot(() => document.body.appendChild(<App />));
    </script>
  </head>
  <body>
  </body>
</html>

Console error:

Uncaught TypeError: Method Date.prototype.toString called on incompatible receiver [object Object]

Maybe the method call isn't bound to the correct object?

@ryansolid
Copy link
Member

ryansolid commented Apr 5, 2019

Oh interesting. It doesn't recognize the Proxy as a Date object. I think it's likely I shouldn't be wrapping Dates in Proxies but you are correct, that it's not being bound to the right object.

I'm just trying to think where else this would apply. I do check if the value is a function on execution anyway so I could always bind functions but that seems perhaps incorrect to do in general.

I don't believe there are any downsides to not wrapping them. I'm not sure if change detection would work properly with mutating them. I think it's fair to treat them as immutable. I think Regex also probably fits into this category. Any opinions on how you'd expect them handled?

EDIT:
Seems MobX has hit this issue before: mobxjs/mobx#662
They treat Date as immutable. So I think I will follow suite.

EDIT 2:
It seems MobX avoids this whole category by not accepting any unknown object which isn't directly inherited from Object. It's clean in the sense it handles a bunch of weird edge cases. It does have bigger implications to consider. Also accessing the prototype has a noticeable performance cost. I think this is a place where a stance has to be taken. I'm taking this under consideration.

@ryansolid ryansolid added the bug Something isn't working label Apr 5, 2019
@ryansolid
Copy link
Member

ryansolid commented Apr 6, 2019

Decided to only wrap plain Objects and Arrays like MobX.

Fixed in v0.4.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants