algolia / react-element-to-jsx-string

Turn a ReactElement into the corresponding JSX string
MIT License
481 stars 79 forks source link

Arrow functions are converted to traditional functions #215

Open ghost opened 6 years ago

ghost commented 6 years ago

Hi, I have the following component:

<Button onClick={e => console.log(e)}>Click me</Button>

But it is transformed to this instead:

<Button
  onClick={function onClick(e) {
                return console.log(e);
            }}
>
  Click me
</Button>

This cast is not only visually bad, it also changes the function behavior since an arrow function passes the this value from the context and not function. We might want to use "this" inside the callback function to alter the parent component state.

By the way, this library is very useful ! Thanks :+1:

armandabric commented 6 years ago

Hi @mbeaudru-mrkl, thanks for your report

After some research it seems an arrow function could be lost if babel enter in the dance. Are your using it?

https://github.com/ljharb/is-arrow-function/issues/6

tomc278 commented 5 years ago

I am using babel and am getting this issue. What would be the best way around it?

armandabric commented 5 years ago

Expect not using Babel, there nothing you can do to avoid the transformation