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

Bug: Unable to Iterate over Symbol Type Properties in React Component #27319

Closed
misaka42 opened this issue Aug 31, 2023 · 3 comments
Closed

Bug: Unable to Iterate over Symbol Type Properties in React Component #27319

misaka42 opened this issue Aug 31, 2023 · 3 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@misaka42
Copy link

React version: all

Steps To Reproduce

import React from "react";

const KEY_OF_A = Symbol.for("a");
const KEY_OF_B = "b";

export default function App() {
  return React.createElement(Foo, {
    [KEY_OF_A]: "A",
    [KEY_OF_B]: "B"
  });
}

function Foo(props) {
  return React.createElement(
    "text",
    null,
    props[KEY_OF_A] + "__" + props[KEY_OF_B]
  );
}


// -> undefined__B

Link to code example: https://codesandbox.io/s/jolly-wright-gzlxf7?file=/src/App.js

The current behavior

for (propName in config) {

   // Remaining properties are added to a new props object
    for (propName in config) {
      if (
        hasOwnProperty.call(config, propName) &&
        !RESERVED_PROPS.hasOwnProperty(propName)
      ) {
        props[propName] = config[propName];
      }
    }

The expected behavior

I'm not sure if it was intended to be designed this way, but I currently have a scenario where I need to inject some private variables into user components from the framework layer. However, these symbols are being removed during the process of passing them. So I'm wondering if it's possible to design this part to support the passing of symbol attributes?

@misaka42 misaka42 added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Aug 31, 2023
@eben-vranken
Copy link

Isn't this just how React normally processes and handles props?

When you use React.createElement and pass props, React essentially serializes and deserializes them. In this process, non-serializable values like Symbols or functions will be lost, as you've noticed.

@misaka42
Copy link
Author

misaka42 commented Sep 1, 2023

@eben-vranken Thank you for your response. But with this scenario we are discussing here is not about passing props from a server component to a client component. i mean there is no process of serializing props via network, We are talking about the normal case of passing props between two React components. In this situation, passing functions (just like event handler) is obviously possible. Therefore, I would like to enhance it to allow the passing properties of symbols.

@sophiebits
Copy link
Collaborator

The goal is for React to pass a props object through unchanged in the future, mainly for performance reasons (reactjs/rfcs#107). At that time, this would work (though likely not with the createElement API directly; rather the new jsx ones that are intended as a compile target).

Using JSX is the idiomatic way to write React and symbol properties are not supported there so I would not really recommend trying to do this anyway. I would recommend using a different property naming convention or finding another way entirely to pass this data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants