react-jsx-parser icon indicating copy to clipboard operation
react-jsx-parser copied to clipboard

Equivalent boolean expressions not evaulated correctly

Open jazzido opened this issue 4 years ago • 3 comments

Hey,

First off, thanks for react-jsx-parser!

I've found a (possible) bug with boolean expressions. In a JSX string, this expression is not evaluated to the expected value:

...
{boundFunction(!boundValue)}
...

This is the workaround, which evaluates as expected:

...
{boundFunction(boundValue ? false : true)}
...

Am I missing anything?

Thanks again!

jazzido avatar Oct 21 '21 16:10 jazzido

@jazzido - you are welcome! I hope the lib is helping you create cool stuff. :)

RE: boolean expressions, I just did a little initial testing, and it does seem like this is getting bound correctly, from what I can tell. I may not be understanding your example fully.

Here's what I can tell is working/parsing correctly:

jsx={'<div data-foo={!true} />'} // correctly renders data-foo="false"
jsx={'<div>{!true}</div>'} // correctly renders <div>false</div>

This tells me that the !{value} parsing is working correctly.

If what you're trying to do is actually invoke the function within JSX, however, you are likely to have a problem because the only things we parse, currently, are simple bindings. The lib does not act as an on-the-fly JS interpreter (which means that it will not correctly interpret your invocation of functions).

TroyAlford avatar Oct 21 '21 18:10 TroyAlford

Thanks for your reply, @TroyAlford.

If what you're trying to do is actually invoke the function within JSX, however, you are likely to have a problem because the only think we parse, currently, are simple bindings. The lib does not act as an on-the-fly JS interpreter (which means that it will not correctly interpret your invocation of functions).

We're indeed invoking a function within JSX, which works as intended. Except for that weird behavior when interpreting its argument.

jazzido avatar Oct 25 '21 12:10 jazzido

In thinking more about this - perhaps the issue, here, is that for some reason we aren't correctly handling the ! operator. The ternary operator is a different construct. :\

TroyAlford avatar Dec 03 '21 18:12 TroyAlford