javascript - react setState : using checkbox to change boolean value

标签 javascript reactjs checkbox boolean

在这里 react 初学者。检查了类似的主题,但没有找到答案。我正在构建一个页面,您可以在其中创建项目和更新信息。但现在我正在为一个简单的复选框而苦苦挣扎。选中后,事件状态应更改为 true,反之亦然。但是,当我单击时,统计数据不会改变。我检查了控制台日志,该功能工作正常,尝试使用 prevState 和简单的 setState active:true,但我仍然无法更改它。我可能遗漏了一些明显的东西,所以如果您能指出来,请提前致谢。

应用程序代码

import React from 'react';
import './App.css';
import ProductList from "../ProductList/ProductList";
import NewProd from "../NewProd/NewProd";

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
            name: "", 
            ean: "", 
            type: "", 
            weight: "", 
            color: "", 
            active: null,
            products: [{name: "Cabbage", ean: "00000000", type: "Vegetable", weight: "2kg", color: "Green", active: false}, 
            {name: "Banana", ean: "111111111", type: "Fruit", weight: "0.3kg", color: "Yellow", active: false}, 
            {name: "Chocolate", ean: "22222222222", type: "Candy", weight: "0.2kg", color: "Brown", active: false}, 
            {name: "Orange", ean: "3333333333", type: "Fruit", weight: "0.5kg", color: "Orange", active: false}, 
            {name: "Cucumber", ean: "4444444444", type: "Vegetable", weight: "1kg", color: "Green", active: false}, ]
    };
    this.isActive = this.isActive.bind(this);
};

handleFormSubmit = (e) => {
  e.preventDefault();
  let products = [...this.state.products];

  products.push({
      name: this.state.name,
      ean: this.state.ean,
      type: this.state.type,
      weight: this.state.weight,
      color: this.state.color,
      active: false,
  });
  this.setState({ products, name: "", ean: "", type: "", weight: "", color: "", active: false}
  );
}
handleInputChange = (e) => {
  let input = e.target;
  let name = e.target.name;
  let value = input.value;
  this.setState({[name]: value})
  };

deleteProduct = (delIndex) => {
  let products = [...this.state.products].filter((product, index) => index !== delIndex);
  this.setState({ products });
};
isActive = () => {
  this.setState({active: !this.state.active})
}
render() {
  return (
    <div className="App">
      <ProductList products={this.state.products} 
      deleteProduct={this.deleteProduct}
      isActive={this.isActive} />
      <NewProd handleFormSubmit={this.handleFormSubmit}
      handleInputChange={this.handleInputChange}
      newName={this.state.name}
      newEan={this.state.ean}
      newType={this.state.type}
      newWeight={this.state.weight}
      newColor={this.state.color} />
    </div>
  );
  }
}

export default App;

列表代码

import React from "react";
import "./ProductList.css";

class ProductList extends React.Component {
    render() { 
        const products = this.props.products;
        return (
            <div>
                <h1>Product List</h1>
                <table>
                    <tr>
                        <th>Name</th>
                        <th>EAN Code</th>
                        <th>Type</th>
                        <th>Weight</th>
                        <th>Color</th>
                        <th>Active</th>
                    </tr>
                    {products.map((product, index) => {
                        return (
                            <tr key={index}>
                                <td>{product.name}</td>
                                <td>{product.ean}</td>
                                <td>{product.type}</td>
                                <td>{product.weight}</td>
                                <td>{product.color}</td>
                                <td><input type="checkbox" onChange={this.props.isActive} /></td>
                                <td><button>View</button></td>
                                <td><button>Edit</button></td>
                                <td><button onClick={() => this.props.deleteProduct(index)}>Delete</button></td>
                            </tr>
                        )
                    })}
                </table>
            </div>
        )
    }
}
export default ProductList;

最佳答案

onChange 需要回调函数,而不是 boolean 值

根据文档,您需要遵循以下方法

<input
  name="isGoing"
  type="checkbox"
  checked={this.state.isGoing} // booean condition here for checked or not
  onChange={this.handleInputChange} />

handleInputChange(event) {
    const target = event.target;
    const value = target.name === 'isGoing' ? target.checked : target.value;
    const name = target.name;

    this.setState({
      [name]: value
    });
  }

关于javascript - react setState : using checkbox to change boolean value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60846972/

相关文章:

javascript - Angular 和 JS 过滤非标准复选框行为

javascript - 组复选框 - 如果未选中任何子框,如何取消选中父框?

javascript - 密文未转换为纯文本且未收到警报

javascript - 调用成功后甚至无法点击 SVG 元素

node.js - socket.io 客户端在 native react 中接收对同一事件的多次调用

reactjs - Gatsby React-helmet 不是服务器端渲染

javascript - React Router v4.2 风格

javafx 2 CheckBoxTableCell 在表格 View 中不起作用

javascript - 在 Chrome DevTools 中同时查看元素和源

javascript - 将徽章添加到 angular prime ng split button 下拉列表