Coder Social home page Coder Social logo

key prop is ignored about jsx-to-string HOT 13 CLOSED

grommet avatar grommet commented on September 17, 2024
key prop is ignored

from jsx-to-string.

Comments (13)

gaearon avatar gaearon commented on September 17, 2024 2

Could you explain the problem to me?

Key is not on props intentionally. This is explained in facebook/react#2429: you don't want to accidentally copy it over to another element, for example. It doesn't behave like a prop, and shouldn't be one.

But a library like jsx-to-string can read element.key without any issues.

from jsx-to-string.

alansouzati avatar alansouzati commented on September 17, 2024 1

Fixed in a48363c.

Released 1.0.1 with support for this.

from jsx-to-string.

alansouzati avatar alansouzati commented on September 17, 2024

Can you please provide an example on how you are trying to call jsx-to-string and what is the expected output. Sorry but I could not understand what the problem is.

from jsx-to-string.

erasmo-marin avatar erasmo-marin commented on September 17, 2024

The problem is that if you do something like this:

let c = <Component>
               <Item key={1}/>
               <Item key={2}/>
        </Component>;

console.log(jsxToString(c));

the output will be:

<Component>
    <Item/>
    <Item/>
</Component>

but should be:

<Component>
    <Item key={1}/>
    <Item key={2}/>
</Component>

from jsx-to-string.

alansouzati avatar alansouzati commented on September 17, 2024

Weird. thanks for sharing. let me try that locally.

from jsx-to-string.

alansouzati avatar alansouzati commented on September 17, 2024

I cannot seem to reproduce it. And even in your site I can see the props are there:

<Container>
  <Box rows={6}>
    <BoxChild wide={1}>
      <div style={{"backgroundColor": "red", "color": "white", "textAlign": "center"}}>
        Hola
      </div>
    </BoxChild>
    <BoxChild wide={2}>
      <div style={{"backgroundColor": "red", "color": "white", "textAlign": "center"}}>
        Soy
      </div>
    </BoxChild>
    <BoxChild wide={1}>
      <div style={{"backgroundColor": "red", "color": "white", "textAlign": "center"}}>
        una
      </div>
    </BoxChild>
    <BoxChild wide={2}>
      <div style={{"backgroundColor": "red", "color": "white", "textAlign": "center"}}>
        grilla
      </div>
    </BoxChild>
    <BoxChild wide={3}>
      <div style={{"backgroundColor": "red", "color": "white", "textAlign": "center"}}>
        con 6 rows
      </div>
    </BoxChild>
    <BoxChild wide={3}>
      <div style={{"backgroundColor": "red", "color": "white", "textAlign": "center"}}>
        y 6 Box.Child
      </div>
    </BoxChild>
  </Box>
</Container>

from jsx-to-string.

erasmo-marin avatar erasmo-marin commented on September 17, 2024

Yep, props are there but the key prop isn't. Use the chrome inspector + React plugin and you will see that every BoxChild has a key prop but it's not displayed. So the problem is not with all the props, but the key prop.

I will try to find out what's the problem, because if you couldn't reproduce it, maybe is related with dependencies versions.

from jsx-to-string.

alansouzati avatar alansouzati commented on September 17, 2024

I see the problem you are facing now.

key is coming straight from react and they hide it in the react props.

I cannot think about a good way of solving this with jsx-to-string. If you have any ideas on how to solve this please advise.

from jsx-to-string.

gaearon avatar gaearon commented on September 17, 2024

To read the key from an element, just read it: element.key. It is not in props, but it is on the element (and that is how React reads it).

from jsx-to-string.

erasmo-marin avatar erasmo-marin commented on September 17, 2024

element.key is undefined and access to it is disabled, but maybe a setting can be added to define a prop name as an alternative to display the key.

It's not a clean solution but it's useful.

from jsx-to-string.

gaearon avatar gaearon commented on September 17, 2024

@erasmo-marin

Access is not “disabled” to element.key. You’ll get a warning if you try to access element.props.key (that doesn’t exist), but <Foo key="123" />.key is "123".

Otherwise React itself wouldn’t be able to read it. 😉

from jsx-to-string.

erasmo-marin avatar erasmo-marin commented on September 17, 2024

EDIT: element.key is not undefined, my mistake

from jsx-to-string.

alansouzati avatar alansouzati commented on September 17, 2024

Thanks @gaearon I will make the modification on jsx-to-string. Did not know that key is on the element so that is great I learned something new :)

from jsx-to-string.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.