Description
Is your feature request related to a problem? Please describe.
Right now with react-easy-state
we can make any functional component or class component reactive, but there's a third type of component called forwardRef
that is not supported.
const state = store({
color: "red",
});
// This is a normal component, reactive thanks to `view`.
const NormalComp = view(() => (
<h3 style={{ color: state.color }}>Normal component</h3>
));
// This is a forwardRef component.
// We can't make it reactive, `view` doesn't work in this case.
const ForwardComp = React.forwardRef((props, ref) => (
<h3 ref={ref} style={{ color: state.color }}>
Forward component
</h3>
));
// This is a forwardRef component from a library.
// We can' make it reacive either, `view` doesn't work in this case.
const H3 = styled.h3`
color: ${state.color};
`;
Describe the solution you'd like
Even though this forwardRef
components are not very common and mostly used by library authors, I think it could be great to have the ability to make them reactive as well.
Describe alternatives you've considered or seen elsewhere
I think the main alternative is to wrap this components inside an additional normal component that is reactive, extract the props that the forwardRef component needs, pass them down and then when it rerenders, rerenders the forwardRef component children as well.
Additional context
This forwardRef
pattern is common on style libraries like styled-components
or emotion
. For example, this is the implementation of styled
in Emotion: https://github.com/emotion-js/emotion/blob/6cb8d15438b01b8623af42f304d5ea3032332187/packages/primitives-core/src/styled.js#L33
The React docs for this are here: https://reactjs.org/docs/forwarding-refs.html
I have made a codesandbox with an example of the issue and the forwardRef components: https://codesandbox.io/s/res-and-forwardref-components-74mvm?file=/src/App.js