From d3253d8c901fc08275c4cc5d322b793acff8cc1e Mon Sep 17 00:00:00 2001 From: bortojac Date: Thu, 29 Mar 2018 23:48:00 -0400 Subject: [PATCH 1/3] close form when new hand clicked - update to React 16.3 - remove componentWillMount in exchange for new static lifestyle method getDerivedStateFromProps - feature implemented by lifting 'active' form state up to index where they are rendered - known issue - Warning received in console claiming that componentWillReceiveProps() is being called in Form component. - Form state is not cleared when clicking the same hand --- components/form.js | 30 ++++++++++++++++++++++-------- components/hand.js | 8 +++++--- package.json | 6 +++--- pages/index.js | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 14 deletions(-) diff --git a/components/form.js b/components/form.js index 8271cf5..9239646 100644 --- a/components/form.js +++ b/components/form.js @@ -3,8 +3,28 @@ import PropTypes from 'prop-types'; import firebase from 'lib/firebase'; class Form extends Component { - constructor() { - super(); + static getDerivedStateFromProps(nextProps, prevState) { + if (nextProps.name !== prevState.hand) { + return { + hand: nextProps.name, + origin: '', + email: '', + comments: '', + }; + } + if (!nextProps.active) { + return { + hand: prevState.hand, + origin: '', + email: '', + comments: '', + }; + } + return null; + } + + constructor(props) { + super(props); this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); this.state = { @@ -15,12 +35,6 @@ class Form extends Component { }; } - componentWillMount() { - this.setState({ - hand: this.props.name, - }); - } - componentDidMount() { this.submissionsRef = firebase.database().ref('submissions'); } diff --git a/components/hand.js b/components/hand.js index f1c3d49..d5869b1 100644 --- a/components/hand.js +++ b/components/hand.js @@ -6,15 +6,14 @@ class Hand extends React.Component { super(props); this.state = { handActive: false, - form: false, }; this.handleClick = this.handleClick.bind(this); } handleClick() { this.setState({ handActive: !this.state.handActive, - form: !this.state.form, }); + this.props.resetFormState(this.props.name); } render() { const isActive = this.state.handActive ? 'active' : ''; @@ -32,7 +31,7 @@ class Hand extends React.Component { {this.props.name} -
+