Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, {Component} from 'react';
- import PropTypes from 'prop-types';
- export default class Redraw extends Component {
- constructor(props) {
- super(props);
- this.infoMessages = {
- warning: 'You cannot use Redraw to return an array of children in v.15.x.x',
- };
- this.state = {
- redrawState: true,
- };
- }
- componentWillMount() {
- const { children } = this.props;
- const { warning } = this.infoMessages;
- if (children instanceof Array) {
- throw new SyntaxError(warning);
- }
- }
- componentWillReceiveProps() {
- this.redrawElement(false);
- }
- shouldComponentUpdate(nextProps, nextState) {
- const { redrawState } = this.state;
- if (nextState.redrawState === redrawState) {
- return false;
- }
- return true;
- }
- componentDidUpdate() {
- this.redrawElement(true);
- }
- redrawElement = (force) => {
- this.setState({
- redrawState: force,
- });
- };
- render() {
- const { children } = this.props;
- const { redrawState } = this.state;
- return (
- redrawState && children
- );
- }
- }
- Redraw.defaultProps = {
- backup: false,
- };
- Redraw.propTypes = {
- backup: PropTypes.bool,
- };
Add Comment
Please, Sign In to add comment