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

Unpossible to use React condition #12

Closed
brezetsky opened this issue May 18, 2017 · 25 comments
Closed

Unpossible to use React condition #12

brezetsky opened this issue May 18, 2017 · 25 comments
Assignees
Milestone

Comments

@brezetsky
Copy link

brezetsky commented May 18, 2017

next structure doesn't work in case of condition is false
{condition && <Separator/> && <Item label="label" disabled/>}

@fkhadra
Copy link
Owner

fkhadra commented May 18, 2017

Can you give more details? I don't get the point here...

@brezetsky
Copy link
Author

Updated. Added like a code this time. Please, look on it one more time.

@fkhadra
Copy link
Owner

fkhadra commented May 18, 2017

if the condition is false with the following structure {condition && <Separator/> && <Item label="label" disabled/>} it will always be false.

If you want to render a separator or an item depending on the condition a ternary is what you are looking for.
condition ? <Separator/> : <Item label="label" disabled/>

image

This has nothing to do with the component, it's pure javascript in that case.

If I miss something please tell me.

@fkhadra fkhadra closed this as completed May 18, 2017
@brezetsky
Copy link
Author

brezetsky commented May 18, 2017

Yes. Your code works. But I would like just if condition. And it something what I have done.
But if condition is true, I would like to add item to menu and without else.

{condition && && } it will always be false. And problem that it produce error.

@brezetsky
Copy link
Author

Like Inline If with Logical && Operator here https://facebook.github.io/react/docs/conditional-rendering.html

@fkhadra
Copy link
Owner

fkhadra commented May 18, 2017

Ok I start to understand now 😁 . If your condition is true you want to render both the <Separator /> and <Item /> ?

@fkhadra
Copy link
Owner

fkhadra commented May 18, 2017

In that case : {condition && <Separator/> && <Item label="label" disabled/>} if the condition is true only
<Item label="label" disabled/> will be rendered

@brezetsky
Copy link
Author

Can not agree with you. If condition is true both of them will rendered and it works perfectly.
But problem if condition return false.
screen shot 2017-05-18 at 2 26 45 pm

@fkhadra
Copy link
Owner

fkhadra commented May 18, 2017

If i do the following : {true && <h1>foo</h1> && <div>bar</div> } only bar is rendered.

Snippet here : codepen.

Can you provide a pen ?

@brezetsky
Copy link
Author

brezetsky commented May 18, 2017

Yes. You are right about true condition
What about false conditions. It's produce a error. And it was my question originally

@fkhadra
Copy link
Owner

fkhadra commented May 18, 2017

What you need to do is :
{false && (<div><h1>foo</h1><div>bar</div></div>)} As you can see you need to wrap the element.
{false && (<h1>foo</h1><div>bar</div>)} . Without the div it won't work.

So in your case, it will be easier if you do because you can't group the separator and the item :

{condition && <Separator />}
{condition && <item />}

snippet : codepen

@brezetsky
Copy link
Author

brezetsky commented May 18, 2017

That is not a problem. Problem exists in that moment when condition is false
Then {condition && < Separator />} returns error
screen shot 2017-05-18 at 2 26 45 pm

@brezetsky
Copy link
Author

brezetsky commented May 18, 2017

And my expectation is not see separator it item if conditions will be false, without any error

@fkhadra
Copy link
Owner

fkhadra commented May 18, 2017

Can you send me the full snippet so I can reproduce the issue ?

@brezetsky
Copy link
Author

brezetsky commented May 19, 2017

Sorry for late answer. Not sure how I can add this plugin to codepen. At least, code for reproducing you can find below.

<ContextMenuProvider id={'context_menu' }>
    Right click
</ContextMenuProvider>
<ContextMenu id={'context_menu' }>
    {enabled ?
       (<Item label="Item 1"/>) :
       (<Item label="Item 1" disabled />)
     }

     {false && <Item label="Item 2 will produce a error" />}
</ContextMenu>

@brezetsky
Copy link
Author

If you can explain me how to add library to codepen - I will add a snippet

@fkhadra fkhadra reopened this May 19, 2017
@fkhadra
Copy link
Owner

fkhadra commented May 19, 2017

@brezetsky the code is enough thanks. I'll check it when I'm home.

@fkhadra fkhadra self-assigned this May 19, 2017
@fkhadra fkhadra added this to the v2.0.0 milestone May 19, 2017
@fkhadra
Copy link
Owner

fkhadra commented May 19, 2017

Hey, in the next release this {false && <Item label="Item 2 will produce a error" />} will work without any issue.I'm almost done

fkhadra added a commit that referenced this issue May 19, 2017
@brezetsky
Copy link
Author

Thanks.
Waiting for new release 👍

@fkhadra
Copy link
Owner

fkhadra commented May 21, 2017

@brezetsky new release published. I still need to update the demo

@fkhadra fkhadra closed this as completed May 21, 2017
@brezetsky
Copy link
Author

@fkhadra problem still appear

@fkhadra
Copy link
Owner

fkhadra commented May 22, 2017

@brezetsky you updated to the last version ?
I'll check when I'm home but it should work. I even included the case inside my test.
But i'll check asap.

@brezetsky
Copy link
Author

Sure.
This works for me #11

@fkhadra
Copy link
Owner

fkhadra commented May 22, 2017

@brezetsky new fix inc is few minutes. My test case was bad !
Fix is live. Sorry for the bug

fkhadra added a commit that referenced this issue May 22, 2017
@brezetsky
Copy link
Author

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants