solidjs / solid-router

A universal router for Solid inspired by Ember and React Router
MIT License
1.1k stars 137 forks source link

[Bug?]: submission.clear is not a function #452

Closed frenzzy closed 3 weeks ago

frenzzy commented 3 weeks ago

Duplicates

Latest version

Current behavior 😯

submission.clear() throws TypeError: submission.clear is not a function

Expected behavior πŸ€”

submission.clear() does not throw

Steps to reproduce πŸ•Ή

Steps:

  1. Create a route with the following content:

    import { action, useSubmission } from '@solidjs/router'
    
    const act = action(async () => {
     'use server'
     await new Promise((resolve) => setTimeout(resolve, 1000))
     return 'OK'
    })
    
    export default function Route() {
     const submission = useSubmission(act)
     return (
       <form action={act} method="post">
         <input type="text" name="name" />
         <button type="submit">Submit</button>
         <button type="button" onClick={() => submission.clear()}>Clear</button>
         <button type="reset">Reset</button>
       </form>
     )
    }
  2. Open the route and click "Clear" button

Context πŸ”¦

Typings says clear can't be undefined: https://github.com/solidjs/solid-router/blob/10b7d8ce03d3aef11074199800bb78ea5fc1b426/src/types.ts#L195-L203

Seams like need to fix typings or always return a clear function.

Your environment 🌎

"@solidjs/router": "^0.13.5"
"@solidjs/start": "^1.0.1"
frenzzy commented 3 weeks ago

Probably I had to report this issue to solid-router, but actually all the methods of useSubmission are undefined by default, even when server returns flash= cookie with action result info, i.e. no javascript case is not working and it is probably solid-start issue.

ryansolid commented 3 weeks ago

Oh.. I see. Yeah this is a router issue. Like the API for useSubmissions assumes an array and it can be empty. However for useSubmission it assume one and it can be undefined. I did it with a proxy so it could dynamically switch under without a accessor function but I'm being too clever here. I should probably have the proxy always return the method even as a no-op. But in general this is an example where I probably didn't take TS into consideration properly because as you said any property could be undefined. I will need to look at what I can do here with TS.