简体   繁体   English

React:更改复选框后如何更新状态

[英]React: How to update state after checkbox is changed

So the idea is like any other car buying website.所以这个想法就像任何其他购车网站一样。 The user checks a box labeled something and the app updates or filters by whatever the label was.用户选中标记为某物的框,应用程序会根据标签进行更新或过滤。 I already have an idea of how to do this but am a little confused on the steps.我已经知道如何做到这一点,但对步骤有点困惑。 How I was thinking it should work would be that, when someone clicks on let's say the SUV checkbox the state is updated to change and checks if the body style is equal to the name of the checkbox.我认为它应该如何工作是,当有人点击假设 SUV 复选框时,状态会更新以更改并检查车身样式是否等于复选框的名称。 Any help would be greatly appreciated.任何帮助将不胜感激。

VehicleList.js
import React, { Component } from "react"
import './App.css';
import Vehicles from "./Vehicles"
import Sort from './Sort';
import { MyProvider, MyContext } from "./Context";

export default class VehicleList extends Component {
  render() {
      return (
        <React.Fragment>
          <Sort />
          <div className="vehicles">
            <div className="showcase">
                  <MyContext.Consumer>
                    {value => {
                      return value.vehicles.map(vehicle => {
                        return <Vehicles key={vehicle.id} vehicle=
                        {vehicle} />
                      })
                    }}
                  </MyContext.Consumer>
              </div>
            </div>
        </React.Fragment>
      )
    }
  }

Sort.js
import React, { Component } from 'react';
import './App.css';


class Sort extends Component {

  render() {
    return (
      <div className="sort-container">
        <h1>Sort By</h1>
        <ul>
          <li style={{ listStyleType: "none"}}><input type="checkbox" name="SUV"/>  SUV</li>
          <li style={{ listStyleType: "none"}}><input type="checkbox" name="Sedan"/>  Sedan</li>
        </ul>
      </div>
    )
  }
}

export default Sort;

As in Sort , the checkbox are multiple choice, so you need to manage the selections in Sort for which checkbox is selected.Sort ,复选框是多选的,因此您需要管理Sort中选中的复选框的选择。 The implementation can be实现可以是
...<input type="checkbox" name="SUV" id=0 onclick=onClicked(this)/> ...

The onClicked function is like onClicked函数就像

function onClicked(checkBox) {
  if (checkBox.checked) {
     this.choices |= 1<<checkBox.id; // choices is a member of Sort
  } else {
     this.choices &= ~(1<<checkBox.id); // de-select
  }
  this.props.onSelected(this.choices); // onSelected is a callback or delegate from VehicleList
}

In VehicleList , replace <Sort /> to <Sort onSelected=onSortSelected/>VehicleList ,将<Sort />替换为<Sort onSelected=onSortSelected/>

onSortSelected is a function of VehicleList . onSortSelected是的函数VehicleList Then you can know what is selected in Sort in onSortSelected .然后你就可以知道在onSortSelectedSort中选择了什么。 Set a state or do whatever you want from the choice.设置一个状态或从选择中做任何你想做的事情。

You could do as @basic suggests and add an onChange containing a setState() .您可以按照@basic 的建议进行操作,并添加一个包含setState()onChange

<li style={{ listStyleType: "none"}}><input type="checkbox" name="SUV" onChange={this.handleChange}/>  SUV</li>

Then handleChange would look something like this:然后handleChange看起来像这样:

handleChange(event){
    if(event.target.checked) {
        this.setState({
          filters: [...filters, event.target.name]
        });
    }
    else {
        let array = [...filters];
        if(array.include(e.target.name)) {          
            array.splice(indexOf(e.target.name);
            this.setState({filters: array)})
        }
    }
}

When a checkbox is checked, it will add the name of the checkbox to an array named filters in the component state.当复选框被选中时,它会将复选框的名称添加到组件状态中名为过滤器的数组中。
When a checkbox is unchecked it will remove it from the array of filters.取消选中复选框时,会将其从过滤器数组中删除。

You should then be able to compare this array of filters to the body of your vehicle.然后,您应该能够将这组过滤器与您的车辆车身进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM