Guest User

Untitled

a guest
Oct 18th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import PropTypes from 'prop-types';
  3.  
  4. export default class Redraw extends Component {
  5. constructor(props) {
  6. super(props);
  7.  
  8. this.infoMessages = {
  9. warning: 'You cannot use Redraw to return an array of children in v.15.x.x',
  10. };
  11.  
  12. this.state = {
  13. redrawState: true,
  14. };
  15. }
  16.  
  17. componentWillMount() {
  18. const { children } = this.props;
  19. const { warning } = this.infoMessages;
  20.  
  21. if (children instanceof Array) {
  22. throw new SyntaxError(warning);
  23. }
  24. }
  25.  
  26. componentWillReceiveProps() {
  27. this.redrawElement(false);
  28. }
  29.  
  30. shouldComponentUpdate(nextProps, nextState) {
  31. const { redrawState } = this.state;
  32.  
  33. if (nextState.redrawState === redrawState) {
  34. return false;
  35. }
  36. return true;
  37. }
  38.  
  39. componentDidUpdate() {
  40. this.redrawElement(true);
  41. }
  42.  
  43. redrawElement = (force) => {
  44. this.setState({
  45. redrawState: force,
  46. });
  47. };
  48.  
  49. render() {
  50. const { children } = this.props;
  51. const { redrawState } = this.state;
  52.  
  53. return (
  54. redrawState && children
  55. );
  56. }
  57. }
  58.  
  59. Redraw.defaultProps = {
  60. backup: false,
  61. };
  62.  
  63. Redraw.propTypes = {
  64. backup: PropTypes.bool,
  65. };
Add Comment
Please, Sign In to add comment