Cannot call `useEffect` in preview component
See original GitHub issueDescribe the bug
I wish to be able to call useEffect
in my code, particularly in the preview ./emails/previews/Foo.tsx
component. I’m not able though - I’m getting this error:
@org/mailing:dev: error - ../../../node_modules/react/cjs/react.development.js (1634:0) @ useEffect
@org/mailing:dev: error - TypeError: Cannot read properties of null (reading 'useEffect')
@org/mailing:dev: at useEffect (webpack-internal:///../../../node_modules/react/cjs/react.development.js:1635:21)
@org/mailing:dev: at Object.Preview (webpack-internal:///./src/moduleManifest.js:663:76)
@org/mailing:dev: at getPreviewComponent (webpack-internal:///./src/util/moduleManifestUtil.ts:24:43)
@org/mailing:dev: at getStaticProps (webpack-internal:///./src/pages/previews/[[...path]].tsx:53:104)
@org/mailing:dev: at Object.renderToHTML (/Volumes/Dev/repos/org-frontend-monorepo/node_modules/next/server/render.tsx:749:20)
@org/mailing:dev: at runMicrotasks (<anonymous>)
@org/mailing:dev: at processTicksAndRejections (node:internal/process/task_queues:96:5)
@org/mailing:dev: at doRender (/Volumes/Dev/repos/org-frontend-monorepo/node_modules/next/server/base-server.ts:1288:30)
@org/mailing:dev: at cacheEntry.responseCache.get.isManualRevalidate.isManualRevalidate (/Volumes/Dev/repos/org-frontend-monorepo/node_modules/next/server/base-server.ts:1429:24)
@org/mailing:dev: at /Volumes/Dev/repos/org-frontend-monorepo/node_modules/next/server/response-cache/index.ts:117:28 {
@org/mailing:dev: page: '/previews/[[...path]]'
@org/mailing:dev: }
To Reproduce Steps to reproduce the behavior:
- Create a preview component for a template
- Add
import { useEffect } from 'react';
- Call
useEffect(() => {
console.log(1);
}, []);
within preview component’s body 4. Crash
Expected behavior Calling hooks should be allowed.
Desktop (please complete the following information):
- OS: [e.g. iOS] macos@12.5.1
- Browser [e.g. chrome, safari] irrelevant but Chrome@106.0.5249.119
- Version [e.g. 22] 0.8.7
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:12
Top Results From Across the Web
React useEffect Invalid hook call. Hooks can only be called ...
I always get this problem when using useEffect but I see others do it just fine. Here is my Code. Component 1 (Parent):...
Read more >Using the Effect Hook - React
The Effect Hook lets you perform side effects in function components: import React, { useState, useEffect } ... Why is useEffect called inside...
Read more >How the useEffect Hook Works (with Examples) - Dave Ceddia
The useEffect hook is the Swiss Army knife of all the hooks. It's the solution to many problems: how to fetch data when...
Read more >"Hooks can only be called inside the body of a function ...
Most common: You're breaking the rules of hooks. You must only call hooks while React is rendering a function component. #The rules of...
Read more >React useEffect cleanup: How and when to use it
Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Oh, that’s a neat use-case I’ve never thought of.
I don’t think there’s a way to accomplish this currently, but we’ve had a few requests for being able to edit props in the GUI and I’d like to support it.
For your use-case it sounds like we need…
Feature 1 needs some design (@monicakogler ❤️) but we should just do it.
Hooks would be a nice way to do (2) but I’m a bit scared of mucking with mjml-react. It looks like this person got it to work but I kind of want the rendered email to use the exact html we’re sending.
Another possibility for (2) would be to allow for async preview functions and let you edit the arguments of the preview functions via the GUI. This might be nice because you could control which fields are editable and we could generate input UI based on types.
One more thought…
https://github.com/sofn-xyz/mailing/issues/136 would solve and I’m happy to prioritize if you want to go that route instead. It’s p2 right now because I don’t think anyone uses it really 😃. I agree this is not the user friendly direction.
@alexfarrill I can confirm your change works as expected. Thank you for the blazing fast🚀 response! 🙂