How can I use `formData` in a react router action?...
# react
z
How can I use
formData
in a react router action? If I call
args.request.formData()
, it complains that it can only be called from a
suspend
function.
ActionLike
can only be
boolean | ActionFunction<Context>
. I'm able to work around it slightly with
flatThen
on
formDataAsync
, but I wouldn't want to do that with more than 2-3 `await`s, which makes me think I'm missing something.
t
Will it support cancellation?
z
Hmm, in my current project, I don't think so. It is entirely client side, with small amounts of predictable data (less than 1KB). But I would like to know how to support cancellation for other projects as well.
t
Let's start from Kotlin Wrappers issue. I will share possible solutions in issue.
z
I've created this issue to discuss it https://github.com/JetBrains/kotlin-wrappers/issues/2812
thank you color 1
t
We updated adapters in latest release! 🎉🎉🎉
@Zyle Moore did you tried new wrappers with cancellation support?
z
Yup, now I'm able to do stuff like this much easier!
Copy code
unsafeJso {
    path = Site.AUTHENTICATE
    action = ActionFunction<Response> { args, handlerCtx ->
        val formData = args.request.formData()
        console.log("action", formData)
        val pilotName = formData.get("pilotName")
        val password = formData.get("password")
        // do some stuff
        PromiseResult(redirect(Site.TimeCards.OVERVIEW))
    }
    Component = CredentialSelector
}
Thanks for the quick turnaround @turansky!
Now I can also do
action = ActionFunction<Response>(::authenticate)
Copy code
suspend fun <Context> authenticate(args: ActionFunctionArgs<Context>, handlerCtx: Any?): DataFunctionValue {
    val formData = args.request.formData()
    console.log("action", formData)
    val pilotName = formData.get("pilotName")
    val password = formData.get("password")
    // do some stuff
    return PromiseResult(redirect(Site.TimeCards.OVERVIEW))
}
t
unsafeJso isn't required in first case - we have strict factory function
In second example PromiseResult isn't expected
z
Hmm, where is that factory function? I would love to use something other than unsafeJso, I'll take a look
t
Factory function has same fqn as type
z
This works really well, thanks @turansky!