Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 30 characters in body; edited tags; edited title
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

Countdown timer written in React.js, react practices

Code is a countdown timer.

Should I keep this update function separate as is or can I save 3 lines of code and embed this.setState({newDeadline: event.target.value}) directly in the callback function below? eg. onChange={event => this.setState({newDeadline: event.target.value}}

import React, { Component } from 'react';
import './App.css';
import Clock from './Clock';
import {FormGroup, FormControl, Button} from 'react-bootstrap';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      deadline: 'December 25 2017',
      newDeadline: ''
    }
  }

  updateDeadline() {
    if (isNaN(Date.parse(this.state.newDeadline)) || this.state.newDeadline === '') {
      this.setState({deadline: this.state.deadline});
    } else {
      this.setState({deadline: this.state.newDeadline})
    }
  }

  updateNewDeadline(event) {
    this.setState({newDeadline: event.target.value})
  }

  render() {
    return (
      <div className="app">
        <div className="app-title">Countdown To {this.state.deadline}</div>
        <Clock deadline={this.state.deadline} />
        <form className="app-form">
          <FormGroup>
            <FormControl className="deadline-input"
              placeholder='Enter a new date'
              onChange={event => this.updateNewDeadline(event)} 
            /> 
            <Button bsStyle="primary" block onClick={() => this.updateDeadline()}>Submit</Button>
          </FormGroup>
        </form>
      </div>
    )
  }
}

export default App;

Countdown timer written in React.js, react practices

Should I keep this update function separate as is or can I save 3 lines of code and embed this.setState({newDeadline: event.target.value}) directly in the callback function below? eg. onChange={event => this.setState({newDeadline: event.target.value}}

import React, { Component } from 'react';
import './App.css';
import Clock from './Clock';
import {FormGroup, FormControl, Button} from 'react-bootstrap';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      deadline: 'December 25 2017',
      newDeadline: ''
    }
  }

  updateDeadline() {
    if (isNaN(Date.parse(this.state.newDeadline)) || this.state.newDeadline === '') {
      this.setState({deadline: this.state.deadline});
    } else {
      this.setState({deadline: this.state.newDeadline})
    }
  }

  updateNewDeadline(event) {
    this.setState({newDeadline: event.target.value})
  }

  render() {
    return (
      <div className="app">
        <div className="app-title">Countdown To {this.state.deadline}</div>
        <Clock deadline={this.state.deadline} />
        <form className="app-form">
          <FormGroup>
            <FormControl className="deadline-input"
              placeholder='Enter a new date'
              onChange={event => this.updateNewDeadline(event)} 
            /> 
            <Button bsStyle="primary" block onClick={() => this.updateDeadline()}>Submit</Button>
          </FormGroup>
        </form>
      </div>
    )
  }
}

export default App;

Countdown timer in React.js

Code is a countdown timer.

Should I keep this update function separate as is or can I save 3 lines of code and embed this.setState({newDeadline: event.target.value}) directly in the callback function below? eg. onChange={event => this.setState({newDeadline: event.target.value}}

import React, { Component } from 'react';
import './App.css';
import Clock from './Clock';
import {FormGroup, FormControl, Button} from 'react-bootstrap';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      deadline: 'December 25 2017',
      newDeadline: ''
    }
  }

  updateDeadline() {
    if (isNaN(Date.parse(this.state.newDeadline)) || this.state.newDeadline === '') {
      this.setState({deadline: this.state.deadline});
    } else {
      this.setState({deadline: this.state.newDeadline})
    }
  }

  updateNewDeadline(event) {
    this.setState({newDeadline: event.target.value})
  }

  render() {
    return (
      <div className="app">
        <div className="app-title">Countdown To {this.state.deadline}</div>
        <Clock deadline={this.state.deadline} />
        <form className="app-form">
          <FormGroup>
            <FormControl className="deadline-input"
              placeholder='Enter a new date'
              onChange={event => this.updateNewDeadline(event)} 
            /> 
            <Button bsStyle="primary" block onClick={() => this.updateDeadline()}>Submit</Button>
          </FormGroup>
        </form>
      </div>
    )
  }
}

export default App;
added 30 characters in body; edited title
Source Link

Countdown timer written in React.js modularity and error, react practices

Source Link

React.js modularity and error

Should I keep this update function separate as is or can I save 3 lines of code and embed this.setState({newDeadline: event.target.value}) directly in the callback function below? eg. onChange={event => this.setState({newDeadline: event.target.value}}

import React, { Component } from 'react';
import './App.css';
import Clock from './Clock';
import {FormGroup, FormControl, Button} from 'react-bootstrap';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      deadline: 'December 25 2017',
      newDeadline: ''
    }
  }

  updateDeadline() {
    if (isNaN(Date.parse(this.state.newDeadline)) || this.state.newDeadline === '') {
      this.setState({deadline: this.state.deadline});
    } else {
      this.setState({deadline: this.state.newDeadline})
    }
  }

  updateNewDeadline(event) {
    this.setState({newDeadline: event.target.value})
  }

  render() {
    return (
      <div className="app">
        <div className="app-title">Countdown To {this.state.deadline}</div>
        <Clock deadline={this.state.deadline} />
        <form className="app-form">
          <FormGroup>
            <FormControl className="deadline-input"
              placeholder='Enter a new date'
              onChange={event => this.updateNewDeadline(event)} 
            /> 
            <Button bsStyle="primary" block onClick={() => this.updateDeadline()}>Submit</Button>
          </FormGroup>
        </form>
      </div>
    )
  }
}

export default App;