Advertisement
beelzebielsk

basic-cat-dog-neither.js

Dec 15th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import logo from './logo.svg';
  3. import './App.css';
  4.  
  5. function CheckMark(props) {
  6.     return (
  7.         <input
  8.             type="checkbox"
  9.             checked={props.checked}
  10.             onChange={props.onChange}/>
  11.     );
  12. }
  13.  
  14. class List extends Component {
  15.     constructor(props) {
  16.         super(props);
  17.         /* I keep track of the states of the checkboxes in this react
  18.          * component. Notice that the checkboxes themselves don't hold
  19.          * their own state: the list tells the checkboxes what they
  20.          * ought to do based on the List's state. It's easier to
  21.          * program this way. With all related state in one place, its
  22.          * easier to check the state of more than one thing at once
  23.          * (which we do), and you know where the state information is
  24.          * coming from. That's the React gospel, at least.
  25.          */
  26.         this.state = {
  27.             cat: false,
  28.             dog: false,
  29.             neither: false,
  30.         };
  31.  
  32.         /* You have to do these things when you're going to pass
  33.          * around the methods of your object as callbacks, and the
  34.          * methods themselves use `this`.
  35.          */
  36.         this.checkDog = this.checkDog.bind(this);
  37.         this.checkCat = this.checkCat.bind(this);
  38.         this.checkNeither = this.checkNeither.bind(this);
  39.     }
  40.  
  41.     checkDog() {
  42.         console.log("Check Dog.");
  43.         this.setState({
  44.             /* This toggles the state of the checkbox, which is what
  45.              * you actually want. If the box is checked, and you click
  46.              * the box again, you want the check to disappear. So you
  47.              * want the state of the box to be the opposite of what it
  48.              * was before. This is known as a "toggle", and this is
  49.              * pretty much how you express toggling.
  50.              */
  51.             dog: !this.state.dog,
  52.             /* Toggling the dog's state also had te effect of setting
  53.              * neither to unchecked-- regardless of its current state.
  54.              * If neither is unchecked, then it will stay unchecked
  55.              * (which is fine), and if neither is checked then it will
  56.              * become unchecked (which is what we want).
  57.              *
  58.              * Note that the above behavior is technically wrong if it
  59.              * were the case that both dog and neither were checked at
  60.              * the same time. I'm not worrying about that case because
  61.              * the only possible way for it to happen is for dog and
  62.              * neither to start checked. There's no possible way to
  63.              * navigate to that state otherwise (theoretically).
  64.              */
  65.             neither: false,
  66.         });
  67.     }
  68.  
  69.     checkCat() {
  70.         console.log("Check Cat.");
  71.         this.setState({
  72.             cat: !this.state.cat,
  73.             neither: false,
  74.         });
  75.     }
  76.  
  77.     checkNeither() {
  78.         console.log("Check Neither.");
  79.         this.setState({
  80.             cat: false,
  81.             dog: false,
  82.             neither: !this.state.neither,
  83.         });
  84.     }
  85.  
  86.     render() {
  87.         console.log(this.state);
  88.         return (
  89.             <ul>
  90.                 <li>{"I like cats (who am I kidding?)"}
  91.                     <CheckMark
  92.                         checked={this.state.cat}
  93.                         onChange={this.checkCat}/>
  94.                 </li>
  95.                 <li>{"I like dogs (I'm gonna blow my brains out)"}
  96.                     <CheckMark
  97.                         checked={this.state.dog}
  98.                         onChange={this.checkDog}/>
  99.                 </li>
  100.                 <li>{"I like neither (the truth... feels good"}
  101.                     <CheckMark
  102.                         checked={this.state.neither}
  103.                         onChange={this.checkNeither}/>
  104.                 </li>
  105.             </ul>
  106.         );
  107.     }
  108. }
  109.  
  110. class App extends Component {
  111.   render() {
  112.       return <List />
  113.   }
  114. }
  115.  
  116. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement