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

useContext does not work #426

Open
maxsalven opened this issue Jan 21, 2023 · 5 comments
Open

useContext does not work #426

maxsalven opened this issue Jan 21, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@maxsalven
Copy link

Describe the bug
useContext throws an error

To Reproduce
Steps to reproduce the behavior:

import React, { createContext, useContext } from "react";
import { Mjml, MjmlBody, MjmlColumn, MjmlSection, MjmlText } from "mjml-react";

const Context = createContext("test");

const Example = () => {
  const value = useContext(Context);
  return (
    <Mjml>
      <MjmlBody>
        <MjmlSection>
          <MjmlColumn>
            <MjmlText>{value}</MjmlText>
          </MjmlColumn>
        </MjmlSection>
      </MjmlBody>
    </Mjml>
  );
};

export default Example;

Expected behavior
An email with the content "test" should render

Screenshots
image

Desktop (please complete the following information):

  • OS: macOS 12
  • Browser Safari
  • Version 16
@alexfarrill alexfarrill added the bug Something isn't working label Jan 23, 2023
@maxsalven
Copy link
Author

If any of the maintainers think this would be reasonably straight forward to fix (cf requiring a major rearchitecture), then I'm happy to dive in and attempt a PR.

@timomeh
Copy link

timomeh commented Apr 25, 2023

As a workaround, you can use a different context-like Library (like Jotai or other libraries which don't rely on React.Context).

I think React's native context doesn't work here because, in the preview server, React is inlined inside the bundle. But when writing import { createContext } from 'react', it doesn't use the same inlined version. React is then complaining about different instances of React being used.

This only affects the preview server. Sending mails with context works fine.

@maxsalven
Copy link
Author

Thanks @timomeh. I haven't tried Jotai v2, but FYI v1 relied on Context and had the same issues.

@timomeh
Copy link

timomeh commented Apr 25, 2023

I'm using this with Jotai 1.10 and it successfully reads values from context, both in the preview as well as inside sendEmail()

@maxsalven
Copy link
Author

Ah great, I must have made a mistake. Will take another look. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants