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

props key returns empty object from scope #183

Open
mattboon opened this issue Aug 23, 2021 · 6 comments · May be fixed by #292
Open

props key returns empty object from scope #183

mattboon opened this issue Aug 23, 2021 · 6 comments · May be fixed by #292
Labels
bug Something isn't working

Comments

@mattboon
Copy link

mattboon commented Aug 23, 2021

Hi there

Have found that using the props key in next-mdx-remote's scope property always returns an empty object

See this example:
https://codesandbox.io/s/next-mdx-remote-props-s85i4?file=/pages/index.js

props.title returns undefined, but props2.title returns as expected. props does however return an empty object.
Could there be something overwriting it somewhere in MDX, or is it somehow a reserved word?

Would ideally like to use the props. notation in MDX pages as it's familiar from the React ecosystem.

Any help appreciated. Thanks

@mattboon mattboon changed the title props key returns empty from scope props key returns empty object from scope Aug 23, 2021
@BRKalow
Copy link
Contributor

BRKalow commented Aug 24, 2021

Good find! From what I can see, with the way the the mdx library is building up the component (reference), we'll have to treat scope.props differently, as injecting props into the scope as we are now understandably conflicts with the local props reference generated by mdx itself.

Effectively what we are doing here is wrapping the result of running mdx in a function to create the scope:

fn (props, props2) {
  return function MDXContent(props) {
    // local props would shadow props from scope
  }
}

I'll do some thinking on a sustainable way to accomplish this. Feel free to contribute if you've got something in mind!

@BRKalow BRKalow added the bug Something isn't working label Aug 24, 2021
@rfrowe
Copy link

rfrowe commented Mar 5, 2022

@BRKalow I've just run into this issue. I have to admit I was quite perplexed when only the props key didn't work. I'm forced to use this codepath with my setup. I'll noodle on this and get back to you with some ideas.

@rfrowe
Copy link

rfrowe commented Mar 5, 2022

Okay I've done a bit of poking around and I came up with a solution that works, though you may not like it. To bypass the shadowing issue, I think we can simply use the props that are passed to MDXContent. I think this intuitively makes sense since we are trying to pass props to the component, so we may as well use the front door rather than the window. For example, if we change the hydration from src/index.tsx:

@@ -70,10 +70,11 @@ export function MDXRemote({
     // if we're ready to render, we can assemble the component tree and let React do its thing
     // first we set up the scope which has to include the mdx custom
     // create element function as well as any components we're using
+    const {props = {}, ...remainingScope} = scope
     const fullScope = Object.assign(
       { opts: { ...mdx, ...runtime } },
       { frontmatter },
-      scope
+      remainingScope
     )
     const keys = Object.keys(fullScope)
     const values = Object.values(fullScope)
@@ -88,7 +89,8 @@ export function MDXRemote({
       keys.concat(`${compiledSource}`)
     )

-    return hydrateFn.apply(hydrateFn, values).default
+    const Component = hydrateFn.apply(hydrateFn, values).default
+    return () => <Component {...props} />
   }, [scope, compiledSource])

   if (!isReadyToRender) {

This will extract any props from scope and inject them into the MDX component via the traditional route. I tested this as a hack via my browser debugger but I'll look into forking this and testing it as a full-fledged change. I think in reality it may make more sense to add a props: P field to MDXRemoteProps<P> and add type parameter <P = {}> to MDXRemote.

@BRKalow
Copy link
Contributor

BRKalow commented May 29, 2022

@rfrowe Sorry for the delay. If you're still interested, feel free to open a PR with this and we can take a further look!

@rfrowe rfrowe linked a pull request Aug 1, 2022 that will close this issue
@rfrowe
Copy link

rfrowe commented Aug 1, 2022

@BRKalow I've opened a PR for this, please take a look.

@talatkuyuk
Copy link

talatkuyuk commented Feb 7, 2024

No need to make any change in the package, the issue comes from the way of implementation.

You can use like props.foo expression in the mdx file by using a recma plugin depicted below.

See mdx-js/mdx#2437

Therecma plugin is recma-mdx-change-props for the issue.

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

Successfully merging a pull request may close this issue.

4 participants