Boolean checkbox value in JSX doesn't work

tafhim :

IU'm using a github code example and that works perfectly with text inputs & numbers, but when I want to use checkbox inputs, the application doesn't understand the values and returns null or just on values...

Any idea why this doesn't work on fEdit function?

import React, { Component } from "react";
// import './App.css';
class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      title: "React Simple CRUD Application",
      act: 0,
      index: "",
      datas: []
    };
  }

  componentDidMount() {
    this.refs.name.focus();
  }

  handleChange = e => {
    console.log(e.target.value);
  };

  fSubmit = e => {
    e.preventDefault();
    console.log("try");

    let datas = this.state.datas;
    let name = this.refs.name.value;
    let isenable = this.refs.isenable.value;

    if (this.state.act === 0) {
      //new
      let data = {
        name,
        isenable
      };
      datas.push(data);
      console.log(data.isenable);
    } else {
      //update
      let index = this.state.index;
      datas[index].name = name;
      datas[index].isenable = isenable;
    }

    this.setState({
      datas: datas,
      act: 0
    });

    this.refs.myForm.reset();
    this.refs.name.focus();
  };

  fRemove = i => {
    let datas = this.state.datas;
    datas.splice(i, 1);
    this.setState({
      datas: datas
    });

    this.refs.myForm.reset();
    this.refs.name.focus();
  };

  fEdit = i => {
    let data = this.state.datas[i];
    this.refs.name.value = data.name;
    this.refs.isenable.value = data.isenable;

    this.setState({
      act: 1,
      index: i
    });

    this.refs.name.focus();
  };

  render() {
    let datas = this.state.datas;
    return (
      <div className="App">
        <h2>{this.state.title}</h2>
        <form ref="myForm" className="myForm">
          <input
            type="text"
            ref="name"
            placeholder="your name"
            className="formField"
          />
          <input
            type="checkbox"
            ref="isenable"
            placeholder="your isenable"
            className="formField"
          />
          <button onClick={e => this.fSubmit(e)} className="myButton">
            submit
          </button>
        </form>
        <pre>
          {datas.map((data, i) => (
            <li key={i} className="myList">
              {data.name} - {(data.isenable || false).toString()}
              <button onClick={() => this.fRemove(i)} className="myListButton">
                remove
              </button>
              <button onClick={() => this.fEdit(i)} className="myListButton">
                edit
              </button>
            </li>
          ))}
        </pre>
      </div>
    );
  }
}

export default App;

MrCode :

When working with checkboxes, refer to the checked property not the value. This is the true or false state of the checkbox.

In fSubmit:

let isenable = this.refs.isenable.checked;

In fEdit:

this.refs.isenable.checked = data.isenable;

In the render:

{data.name} - {data.isenable ? 'on' : 'off'}

Working demo

Complete code with fixes:

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      title: "React Simple CRUD Application",
      act: 0,
      index: "",
      datas: []
    };
  }

  componentDidMount() {
    this.refs.name.focus();
  }

  handleChange = e => {
    console.log(e.target.value);
  };

  fSubmit = e => {
    e.preventDefault();
    console.log("try");

    let datas = this.state.datas;
    let name = this.refs.name.value;
    let isenable = this.refs.isenable.checked;

    if (this.state.act === 0) {
      //new
      let data = {
        name,
        isenable
      };
      datas.push(data);
      console.log(data.isenable);
    } else {
      //update
      let index = this.state.index;
      datas[index].name = name;
      datas[index].isenable = isenable;
    }

    this.setState({
      datas: datas,
      act: 0
    });

    this.refs.myForm.reset();
    this.refs.name.focus();
  };

  fRemove = i => {
    let datas = this.state.datas;
    datas.splice(i, 1);
    this.setState({
      datas: datas
    });

    this.refs.myForm.reset();
    this.refs.name.focus();
  };

  fEdit = i => {
    let data = this.state.datas[i];
    this.refs.name.value = data.name;
    this.refs.isenable.checked = data.isenable;

    this.setState({
      act: 1,
      index: i
    });

    this.refs.name.focus();
  };

  render() {
    let datas = this.state.datas;
    return (
      <div className="App">
        <h2>{this.state.title}</h2>
        <form ref="myForm" className="myForm">
          <input
            type="text"
            ref="name"
            placeholder="your name"
            className="formField"
          />
          <input
            type="checkbox"
            ref="isenable"
            placeholder="your isenable"
            className="formField"
          />
          <button onClick={e => this.fSubmit(e)} className="myButton">
            submit
          </button>
        </form>
        <pre>
          {datas.map((data, i) => (
            <li key={i} className="myList">
              {data.name} - {data.isenable ? 'on' : 'off'}
              <button onClick={() => this.fRemove(i)} className="myListButton">
                remove
              </button>
              <button onClick={() => this.fEdit(i)} className="myListButton">
                edit
              </button>
            </li>
          ))}
        </pre>
      </div>
    );
  }
}

Sidenote: I wouldn't use Refs in this case. Take a look at the docs, you can see when to use Refs and when not to. The Forms docs cover how to handle forms without Refs.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=11413&siteId=1