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

wp-each "Cannot access 'state' before initialization" #69266

Open
3 of 6 tasks
poof86 opened this issue Feb 20, 2025 · 1 comment
Open
3 of 6 tasks

wp-each "Cannot access 'state' before initialization" #69266

poof86 opened this issue Feb 20, 2025 · 1 comment
Labels
[Type] Bug An existing feature does not function as intended

Comments

@poof86
Copy link

poof86 commented Feb 20, 2025

Description

I've updated gutenberg from a v18... to the current v20.2.0 and it seems something has changed with how the state is initialised before directives are parsed.

My wp-each directive referencing a state derived getter wasn't a problem before. Now when the wp-each directive is parsed and calls a getter on the state that uses the state variable I'm getting the error: "ReferenceError: Cannot access 'state' before initialization"

I had to wrap the first access to the state in the getter with a try catch to find out this was the problem.

Step-by-step reproduction instructions

  • Create a state getter in which you reference the state
  • Use this getter in a data-wp-each
  • Wrap the line where you access the state in the getter with a try..catch to catch the error

Screenshots, screen recording, code snippet

Example:

view.js state getter snippet

const { state } = store('mynamespace/query', {
state: {
	get terms() {
        	try {
			const { taxonomy : taxonomyQuery = {} } = state;
			const { taxonomy } = getContext();
			return taxonomyQuery[taxonomy] ?? [];
        	} catch (error) {
			console.log(error);
			return [];
		}
	}
}

render.php snippet

<template data-wp-each="state.terms">
	<a 
		data-wp-text="state.termLabel"
	></a>
</template>

Environment info

Wordpress 6.7.2
Gutenberg 20.2.0
Custom theme

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • Yes

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure
@poof86 poof86 added the [Type] Bug An existing feature does not function as intended label Feb 20, 2025
@poof86
Copy link
Author

poof86 commented Feb 20, 2025

I've been trying to figure out long before posting this issue why my code stopped working, and of course just after posting it I found a (temporary) solution. Simply get the state reference before creating your actual state implementation:

const { state } = store('mynamespace/query');

store('mynamespace/query', {
	 state: {
	 	 get terms() {
...

But this shouldn't be needed right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

1 participant