Description
Describe the feature you'd like:
Enhance the rerender
usage to not require the use of a Javascript getter function for properties of result
.
It took approximately 45 minutes of digging through code examples, blogs, RHTL code, and a substantial number of console logs to figure out why:
const { result: { all }, rerender } = renderHook(myHook);
rerender();
console.log(all.length); // always 1
As it turns out, a console.log of the entire return value of renderHook was what I needed (I got desperate and was looking for undocumented return values).
{
result: { all: [Getter], current: [Getter], error: [Getter] },
rerender: [Function: rerenderHook],
unmount: [Function: unmountHook],
waitFor: [AsyncFunction: waitFor],
waitForValueToChange: [AsyncFunction: waitForValueToChange],
waitForNextUpdate: [AsyncFunction: waitForNextUpdate]
}
The implementation of result
relying on getters means I effectively cannot destructure the result, which feels like common practice in the JS community.
Suggested implementation:
It'd be excellent to return the newly generated result from rerender
instead of it being void. That way I can do:
const { result: { current: initial }, rerender } = renderHook(myHook);
const { result: { current: subsequent }} = rerender();
Another alternative is to make the all reference a single array reference throughout to preserve the results & allow the destructure to reference the collection. This wouldn't work, however, for current or error.
Teachability, Documentation, Adoption, Migration Strategy:
At a minimum, the documentation for the RenderResult should be updated to make it clear that it should not be destructured given the implementation details.