Replies: 1 comment
-
|
I actually ran into this myself in a project I'm working on. My solution was to wrap it in an object, that this, |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Writing this in the hopes I rescue at least one person from what I spent the last 6 hours debugging.
Cap'n Web + React: Avoid the
'' is not a functionTrapThis note documents a subtle failure mode we hit while building an API between browsers and Durable Objects with Cap'n Web. It can affect any React-driven client that stores a Cap'n Web stub returned from
RpcSession#getRemoteMain().Where the Problem Shows Up
React checks the type of the argument passed to
setStub. If it is a function, it assumes you are providing a state updater (setState(prev => next)) and invokes it immediately with the previous value (prev). Cap'n Web stubs are proxies masquerading as callables, so React triggers a bogus remote invocation with the previous stub value (oftennull). That leads to wire messages like:…which fail inside Cap'n Web with the familiar:
On the surface it looks like a Cap'n Web or transport bug, but the real issue is the React state API mistaking the stub for an updater.
The Fix
Always wrap callable stubs (or any other function-like values) in a function when passing them to a React setter:
This version passes a function that returns the stub instead of the stub itself. React now treats it as “updater returning new state” and does not execute the stub.
Symptoms & Diagnosis Checklist
TypeError: '' is not a function.whenever a Cap'n Web method is invoked.["pipeline",0,[],[null]]followed by a release of the same import id.If those conditions match, check your
setStatecall.Quick Reference
setCapnwebStub(() => remoteStub)setCapnwebStub(remoteStub)Why Document This Here?
Browser-facing uses of Cap'n web that use React can easily rely on Cap'n Web stubs inside React components or hooks, intentionally or by accident. Anyone using Cap'n Web to connect users to back-ends could experience the same failure mode. This guide captures the root cause and its fix so others can avoid the six-hour debugging session that this author experienced to solve this problem!
Beta Was this translation helpful? Give feedback.
All reactions