简体   繁体   English

处理程序无法在React的子组件中按预期方式工作

[英]Handler not working as expected in child component in React

I am on the last spec in a section of a test from school. 我在学校考试的一部分中处于最后一个规范。

select should have an onChange event that submits the new animal select应该有一个onChange事件来提交新动物

Essentially I can't seem to get the handleSubmit function passed into the child, to respond to the changes... This is the rest of the spec. 从本质上讲,我似乎无法将handleSubmit函数传递给子级以响应更改...这是规范的其余部分。

it('select should have an onChange event that submits the new animal', () => {
  expect(animalSelect.props('select').onChange).to.be.function;

  // choosing a random animal
  let animal = getRandomAnimal();

   // simulating a 'change' event with an event described as the second argument given to `simulate`
   animalSelect.find('select').simulate('change', { target: { value: animal } });

   // the spy sent in should be called with the argument described
   expect(setAnimalSpy.calledWith(animal)).to.be.true;
 });

This is the parent component Exhibit : 这是父组件Exhibit

import React, { Component } from 'react';
import AnimalSelect from './AnimalSelect';
import Cage from './Cage';

export default class Exhibit extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedAnimal: this.props.selectedAnimal,
    };
    this.setAnimal = this.setAnimal.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  setAnimal(animal) {
    this.setState({ selectedAnimal: animal });
  }

  handleSubmit(event) {
    this.setState({ selectedAnimal: event.target.value });
  }

  render() {
    const { selectedAnimal } = this.state;
    return (
      <div className="exhibit">
        <AnimalSelect handleSubmit={this.handleSubmit} submitAnimal={this.setAnimal} animals={this.props.animals} />
        <Cage selectedAnimal={selectedAnimal} />
      </div>
    );
  }
}

This is the AnimalSelect (The child of Exhibit ) component: 这是AnimalSelectExhibit的子代)组件:

import React, { Component } from 'react';

// exporting the constructor function (dumb component).
// what is the parameter coming in here?
export default function AnimalSelect(props) {
  // prettier-ignore
  return (
    <form>
      <label>Select an Animal: </label>
      <select onChange={() => props.handleSubmit}>
        {props.animals.map((animal, index) => {
          return (
            <option key={animal} value={animal}>
              {animal}
            </option>
          );
        })}
      </select>;
    </form>
  );
}

Unfortunately this is the only error I am getting. 不幸的是,这是我遇到的唯一错误。

    AssertionError: expected false to be true

Any ideas? 有任何想法吗?

Here, you set the event handler to be an anonymous function which returns a reference to a function: 在这里,将事件处理程序设置为匿名函数,该函数返回对该函数的引用:

<select onChange={() => props.handleSubmit}>

You probably intend something more like this: 您可能打算更像这样:

<select onChange={evt => props.handleSubmit(evt)}>

This effectively delegates the event handler to the parent component's function, passing the event object along to it. 这有效地将事件处理程序委托给父组件的功能,并将事件对象传递给它。 Although I'm uncertain why setting the handler as suggested in the comment didn't work. 尽管我不确定为什么按注释中的建议设置处理程序无效。

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

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