Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transform-hook): UseTransformComponent added #405

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tylerlaws0n
Copy link

Hook added to return an element, given the current state

Given that v3 introduces the dynamic where only useTransformEffect is the only hook that has up to date state

https://github.com/prc5/react-zoom-pan-pinch/blob/c0871e27907c1e9338c613d1e5abfc98a873da52/CHANGELOG.md?plain=1#L5-L7

I wanted to suggest a new style of hook that could return an item, given the internal context.

The reason for this is the following pattern.

Currently if I want to write a component that uses state from inside the context I need to copy that state using useTransformEffect to another piece of state to use it like this:

const MyComponent: React.FC = () => {
  const [scale, setScale] = useState(0);

  useTransformEffect(({ state }) => {
    const currentScale = state.scale;

    setScale(currentScale);
  });

  return <div>My Scale is: {scale}</div>;
}

This works for what I am trying to do, but I have run into issues with synchronization and infinite loops when duplicating state like this.

The new hook I am proposing here should allow something like this:

const MyComponent: React.FC = () => {
  return useTransformComponent(({ state }) => {
    const currentScale = state.scale;

     return <div>My Scale is: {currentScale}</div>;
  });
}

This would remove the duplicated state and ensure that the same state being rendered on the page is the state coming from the context.

I can add docs, tests, and examples, but I wanted to see what the response from y'all was like before adding any of that.

Hook added to return an element, given the current state
@tylerlaws0n tylerlaws0n requested a review from prc5 as a code owner August 8, 2023 21:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant