A handler is a method read off state
The message
MARKLESS_STATE_CLASS_METHOD_UNBOUND: `<attributeName>={<referenceSource>}` hands the event a method read off state "<stateName>". A value in state is data, so the handler would have nothing to call, and a method passed without its object runs with no `this`.
state() crosses from the server to the browser as data; methods do not cross with it. A handler must be a function the compiler can see, so it can run in the browser after resume.
Write `<attributeName>={() => <referenceSource>()}`, so the call is visible where the event runs.
https://markless.dev/errors/MARKLESS_STATE_CLASS_METHOD_UNBOUNDWords in angle brackets change from case to case.
Why
onClick={cart.add} hands the event a method of an instance that state holds. The method does not reach the browser, and even in plain JavaScript a method passed without its object runs with no this. Only element event attributes are checked: items.forEach(cart.add) and a component prop such as onPick={cart.add} are not. What the build does not catch yet: an instance returned from a function or a static factory (state(makeCart()), Cart.create()), one nested in an array of arrays, inside an object in an array, behind a condition (ok ? new Cart() : null), as a Map value, under a computed key, made with Object.create, or assigned or pushed later in a handler (cart = new Cart(), carts.push(new Cart())). Those still build, then fail at server render or leave the page stale.
Fix
Call the method inside an arrow function, onClick={() => cart.add()}, and keep the data plain as the page for MARKLESS_STATE_CLASS_INSTANCE describes.
Related
Where Markless raises it
Button sounds are from Josh Comeau's Button Sounds library.