question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

props key returns empty object from scope

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
rfrowecommented, 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.

0reactions
rfrowecommented, Aug 1, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prop from parent component returns empty on one of the childs
I found the problem, on the GSearchVehicle component my memoized functions were missing the positions prop on it's dependencies.
Read more >
How to Check If an Object is Empty in JavaScript? - Scaler
If the method returns an empty array, then it means the given object is empty as it has no keys. This way we...
Read more >
JavaScript: Check if an Object is Empty - Stack Abuse
keys () is a static method that returns an Array when we pass an object to it, which contains the property names (keys)...
Read more >
typescript-cheatsheet - GitHub Pages
argument types function multiply(value1: number, value2: number) { return ... In JavaScript, so as in TypeScript, Objects are comprised of key-value pairs.
Read more >
JSX In Depth - React
createElement , the React library must also always be in scope from your JSX code. ... JSX type can't be an expression. return...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found