Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the React.js way of handling visibility=hidden?

Tags:

reactjs

React does not support the visibility attribute for elements.

So if I want to show or hide an element on the page, but still have it take up space when hidden so the layout doesn't shift, how do I do something like this?

<i className="fa fa-trash" visibility={this.state.showButton ? "visible": "hidden"} />
like image 410
Richard Avatar asked Nov 12 '15 09:11

Richard


People also ask

What is the use of visibility hidden?

Using visibility: hidden hides an element from the browser; however, that hidden element still lives in the source code. Basically, visibility: hidden makes the element invisible to the browser, but it still remains in place and takes up the same space had you not hidden it.

What is visibility sensor in React?

Sensor component for React that notifies you when it goes in or out of the window viewport.


3 Answers

You can use CSS for this.

<i className="fa fa-trash" style={{visibility: this.state.showButton ? 'visible' : 'hidden' }} />

Learn more about inline styles in React

like image 94
David Xu Avatar answered Oct 06 '22 16:10

David Xu


You can use a CSS class for that and dynamically modify your className. For example:

<i className={this.state.showButton ? "fa fa-trash" : "fa fa-trash hidden"} />
like image 41
Emilio Rodriguez Avatar answered Oct 06 '22 15:10

Emilio Rodriguez


This is a css-attribute so you can use inline-styles:

...
var visibilityState = this.state.showButton ? "visible" : "hidden";
return (
  <i className="fa fa-trash" style={{visibility: visibilityState}}/>
);
...
like image 4
Slowyn Avatar answered Oct 06 '22 14:10

Slowyn