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

Docs/iAPI: Fix wrong code snippet in data-wp-run example #62835

Merged

Conversation

luisherranz
Copy link
Member

What?

Fix a wrong code snippet in the data-wp-run example.

Discovered by @fabiankaegy and reported in Slack: https://wordpress.slack.com/archives/C071CRKGKUP/p1719289120450429

Why?

The data-wp-run callbacks run in the body of the Preact component, and ref is basically a useRef hook, which means that ref is null on the first render.

All this means that, in contrary to how it works in data-wp-watch and data-wp-init, which are basically fancy versions of useEffect, you need to access ref inside a useEffect-like hook (useEffect, useInit, useWatch…) when you use data-wp-run, exactly like you would use it in (P)React:

const Comp = () => {
  const ref = useRef();
  console.log(ref.current); // null on first render
  useEffect(() => {
    console.log(ref.current); // it's fine here
  });
  return <div ref={ref}>...</div>;
};

How?

In this specific example, I moved the getElement() call to the useEffect hook of the useInView hook, so ref is not null anymore.

@luisherranz luisherranz added [Type] Developer Documentation Documentation for developers [Feature] Interactivity API API to add frontend interactivity to blocks. labels Jun 25, 2024
@luisherranz luisherranz self-assigned this Jun 25, 2024
@luisherranz
Copy link
Member Author

I'm going to add a paragraph to explain how ref works within data-wp-run as well.

@luisherranz luisherranz marked this pull request as ready for review June 25, 2024 13:40
Copy link

github-actions bot commented Jun 25, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: luisherranz <luisherranz@git.wordpress.org>
Co-authored-by: fabiankaegy <fabiankaegy@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Member

@fabiankaegy fabiankaegy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates here :)

@luisherranz luisherranz enabled auto-merge (squash) June 25, 2024 13:52
@luisherranz luisherranz merged commit 90bc142 into trunk Jun 25, 2024
68 checks passed
@luisherranz luisherranz deleted the docs/interactivity-api-fix-wrong-wp-run-code-snippet branch June 25, 2024 14:13
@github-actions github-actions bot added this to the Gutenberg 18.7 milestone Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Interactivity API API to add frontend interactivity to blocks. [Type] Developer Documentation Documentation for developers
2 participants