React Hello World Example with ES6+JSX
A basic Hello World widget with in browser JSX transformed + ES6 support
by Nevin Madhukar
March 06, 2017
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.1/JSXTransformer.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.1/react-with-addons.js"></script>
<script src="https://facebook.github.io/react/js/jsfiddle-integration.js"></script>
<div id="container">
<!-- This element's contents will be replaced with your component. -->
</div>
CSS
html,
body {
margin:0;
padding:0;
height:100%;
}
.group_page_header {
width: 100%;
height: 20px;
background-color: #0b97c4;
position: fixed;
z-index: 1;
}
.body_clr {
overflow-y: scroll;
width: 100%;
height: 100%;
display: flex;
position: relative;
background-color: #eceff1;
}
.other-stuff{
height: 1000px;
display: block;
background-color: yellow;
width: 100%;
}
JavaScript 1.7
class InternalGroupsPage extends React.Component {
constructor() {
super();
}
componentDidMount() {
window.addEventListener('scroll', this.handleScroll);
};
componentWillUnmount() {
window.removeEventListener('scroll', this.handleScroll);
};
getScrollDirection() {
var currentScrollTop = document.body.scrollTop();
var isScrollUp = currentScrollTop < this.previousScrollTop;
this.previousScrollTop = currentScrollTop;
return isScrollUp;
}
shouldComponentUpdate(nextProps, nextState) {
return this.state.hideHeader !== nextState.hideHeader;
}
// scroll down -> show
// scroll up -> hide
handleScroll(event) {
let scrollTop = event.srcElement.body.scrollTop,
const isScrollUp = this.getScrollDirection();
if(isScrollUp) {
this.setState({
hideHeader: true,
});
} else {
this.setState({
hideHeader: false,
});
}
},
render() {
const { hideHeader } = this.state;
const headerClassName = hideHeader ? `group_page_header hideHeader` : `group_page_header showHeader`;
return (
<div className="body_clr">
<div className={headerClassName}>
This is the div i want to change the height when scrolling
</div>
<div className="other-stuff">
Other stuff
</div>
</div>
);
}
}
React.render(<InternalGroupsPage />, document.getElementById('container'));