How to Mount and Unmount React Components

Size
3,734 Kb
Views
26,312

How do I make an how to mount and unmount react components?

A simple example of a mountable component that illustrates the component lifecycle of React components and how to use them. . What is a how to mount and unmount react components? How do you make a how to mount and unmount react components? This script and codes were developed by Joshua Michael Waggoner on 11 October 2022, Tuesday.

How to Mount and Unmount React Components Previews

How to Mount and Unmount React Components - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>How to Mount and Unmount React Components</title>
<script src="https://fb.me/react-dom-0.14.6.js"></script>
<link href='https://fonts.googleapis.com/css?family=UnifrakturMaguntia|Comfortaa|Lato' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <!-- App -->
<div id="app">
</div> <script src='http://cdnjs.cloudflare.com/ajax/libs/react/0.14.3/react.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

How to Mount and Unmount React Components - Script Codes CSS Codes

html { background-color: #333333; color: white;
}

How to Mount and Unmount React Components - Script Codes JS Codes

'use strict';
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
/** * An example of how to mount and unmount components in React.js * Author: Joshua Michael Waggoner * <[email protected]> */
// Styles are irrelevant, but want to show how
// you can inject them into inline styles in JSX
var styles = { width: '150px', height: '50px', margin: '10px', border: 'solid 2px black', fontFamily: 'Lato', fontSize: '30px', color: 'black', backgroundColor: 'white'
};
var h1styles = { margin: '10px', fontFamily: 'Lato', fontSize: '30px', color: '#FF3333'
};
// Counter component
var Counter = function (_React$Component) { _inherits(Counter, _React$Component); function Counter() { _classCallCheck(this, Counter); var _this = _possibleConstructorReturn(this, _React$Component.call(this)); _this.state = { val: 0 }; _this.update = _this.update.bind(_this); return _this; } Counter.prototype.update = function update() { this.setState({ val: this.state.val + 1 }); }; Counter.prototype.componentWillMount = function componentWillMount() { console.log('mounting'); }; Counter.prototype.render = function render() { console.log('rendering!'); return React.createElement( 'button', { style: styles, onClick: this.update }, this.state.val ); }; Counter.prototype.componentDidMount = function componentDidMount() { console.log('mounted'); }; Counter.prototype.componentWillUnmount = function componentWillUnmount() { console.log('bye!'); }; return Counter;
}(React.Component);
// Wrapper component
var Wrapper = function (_React$Component2) { _inherits(Wrapper, _React$Component2); function Wrapper() { _classCallCheck(this, Wrapper); return _possibleConstructorReturn(this, _React$Component2.call(this)); } Wrapper.prototype.mount = function mount() { React.render(React.createElement(Counter, null), document.getElementById('a')); }; Wrapper.prototype.unmount = function unmount() { React.unmountComponentAtNode(document.getElementById('a')); }; Wrapper.prototype.render = function render() { return React.createElement( 'div', null, React.createElement( 'h1', { style: h1styles }, 'How to Mount/Unmount a Component in React.js' ), React.createElement('hr', null), React.createElement( 'button', { style: styles, onClick: this.mount.bind(this) }, 'Mount' ), React.createElement( 'button', { style: styles, onClick: this.unmount.bind(this) }, 'Unmount' ), React.createElement('div', { id: 'a' }) ); }; return Wrapper;
}(React.Component);
// Render Wrapper
React.render(React.createElement(Wrapper, null), document.getElementById('app'));
How to Mount and Unmount React Components - Script Codes
How to Mount and Unmount React Components - Script Codes
Home Page Home
Developer Joshua Michael Waggoner
Username rabbitfighter81
Uploaded October 11, 2022
Rating 3
Size 3,734 Kb
Views 26,312
Do you need developer help for How to Mount and Unmount React Components?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Joshua Michael Waggoner (rabbitfighter81) Script Codes
Create amazing SEO content with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!