You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Check if updating to the latest Preact version resolves the issue
Describe the bug
When trying to use vanilla JS and preact/compat
it fails with undefined currentComponent.__hooks
in function getHookState(index, type)
under Firefox and Chromium
To Reproduce
simple code with js modules:
<!DOCTYPE html><html><head><metacharset="UTF-8" /><title>PReact Hello World</title></head><body><divid="root"></div><scripttype="importmap">{"imports": {"preact": "https://unpkg.com/preact@10.17.1/dist/preact.mjs","preact/hooks": "https://unpkg.com/preact@10.17.1/hooks/dist/hooks.mjs","react": "https://unpkg.com/preact@10.17.1/compat/dist/compat.mjs","react-dom": "https://unpkg.com/preact@10.17.1/compat/dist/compat.mjs"}}</script><scripttype="module">importReactfrom'react';importReactDOMfrom"react-dom";functioncounter(){// State to store count valueconst[count,setCount]=React.useState(0);// Function to increment count by 1constincrementCount=()=>{// Update state with incremented valuesetCount(count+1);};returnReact.createElement("div",{className: "app"},React.createElement("button",{onClick: incrementCount},"Click Here"),count);}constrootElement=document.getElementById('root');constelement=counter();preact.createRoot(rootElement).render(element);</script></body></html>
Steps to reproduce the behavior:
create an html page with the code above and open it in the browser
check the console
Expected behavior
there should not be syntax errors and the code should render a preact component
The text was updated successfully, but these errors were encountered:
@GeorgeRadev Component functions cannot be called directly. They expect to be called in the context of the rendering framework. If you just call them outside that the whole rendering context needed for hooks and things will not be present, hence the error.
functionApp(){return<h1>hello world</h1>}// BAD: Don't do this, component is called eagerly outside of// the rendering context.render(App(),document.getElementById("app"));// CORRECTrender(<App/>,document.getElementById("app"));
Applying the fix to your snippet:
- const element = counter();- preact.createRoot(rootElement).render(element);+ const element = React.createElement(counter, null);+ ReactDom.render(element, rootElement); // React v18 APIs are not supported in compat
FYI: You can also simplify your import map quite a bit by going with esm.sh:
Thank you @marvinhagemeister,
The fastest, accurate and understandable response.
Now it make sense that the function needs to be executed when the rendering is happening, the result of it has no use bore that.
Thank you once again
Describe the bug
When trying to use vanilla JS and preact/compat
it fails with undefined currentComponent.__hooks
in function getHookState(index, type)
under Firefox and Chromium
To Reproduce
simple code with js modules:
Steps to reproduce the behavior:
Expected behavior
there should not be syntax errors and the code should render a preact component
The text was updated successfully, but these errors were encountered: