javascript - 需要 Reactjs 帮助来了解如何正确地将这个函数从父按钮组件传递到子按钮组件

标签 javascript reactjs function components

对于reactjs来说是全新的,所以请耐心等待。
- 我有一个返回表格的 MovieContainer 组件。
- 我有一个子 MovieCell 组件,它将放置在 MovieContainer 内并迭代对象数组。单元格内部有一个删除按钮,它将过滤列表数组以删除单击的项目
- 我在 MovieContainer 组件中准备了删除函数,但我不确定将其传递给子组件的正确方法。

github link
MovieContainer 组件:

class MoviesContainer extends Component {
  constructor(props) {
    super(props);

    this.state = {
      movies: getMovies()
    };

    this.deleteMovie = this.deleteMovie.bind(this);
  }

  deleteMovie(movie) {
    const movies = this.state.movies.filter(m => m._id !== movie._id);
    this.setState({
      movies: movies
    });
    console.log(this);
  }

  render() {
    return (
      <table className="table">
        <thead>
          <tr>
            <th>Title</th>
            <th>Genre</th>
            <th>Stock</th>
            <th>Rate</th>
          </tr>
        </thead>
        <tbody>
          {this.state.movies.map(movie => (
            <MovieCell
              key={movie._id}
              title={movie.title}
              genre={movie.genre.name}
              numberInStock={movie.numberInStock}
              dailyRentalRate={movie.dailyRentalRate}
              deleteMovie={this.deleteMovie}
            />
          ))}
        </tbody>
      </table>
    );
  }
}

MovieCell 组件:

class MovieCell extends Component {
  constructor(props) {
    super(props);

    this.handleDelete = this.handleDelete.bind(this);
  }

  handleDelete(movie) {
    this.props.deleteMovie(movie);
  }

  render() {
    const title = this.props.title;
    const genre = this.props.genre;
    const numberInStock = this.props.numberInStock;
    const dailyRentalRate = this.props.dailyRentalRate;

    return (
      <tr>
        <td>{title}</td>
        <td>{genre}</td>
        <td>{numberInStock}</td>
        <td>{dailyRentalRate}</td>
        <td>
          <button onClick={movie => this.handleDelete(movie)}>Delete</button>
        </td>
      </tr>
    );
  }
}

最佳答案

您当前将事件命名为movie并将其用作电影对象,但这将无法按预期工作。

您可以将电影 ID 作为 Prop 传递,并在调用 handleDelete 时使用它来代替 event

class MoviesContainer extends Component {
  // ...

  deleteMovie(movieId) {
    const movies = this.state.movies.filter(m => m._id !== movieId);
    this.setState({
      movies: movies
    });
  }

  render() {
    return (
      <table className="table">
        <thead>
          <tr>
            <th>Title</th>
            <th>Genre</th>
            <th>Stock</th>
            <th>Rate</th>
          </tr>
        </thead>
        <tbody>
          {this.state.movies.map(movie => (
            <MovieCell
              key={movie._id}
              id={movie._id}
              title={movie.title}
              genre={movie.genre.name}
              numberInStock={movie.numberInStock}
              dailyRentalRate={movie.dailyRentalRate}
              deleteMovie={this.deleteMovie}
            />
          ))}
        </tbody>
      </table>
    );
  }
}

class MovieCell extends Component {
  // ...

  handleDelete() {
    this.props.deleteMovie(this.props.id);
  }

  render() {
    const title = this.props.title;
    const genre = this.props.genre;
    const numberInStock = this.props.numberInStock;
    const dailyRentalRate = this.props.dailyRentalRate;

    return (
      <tr>
        <td>{title}</td>
        <td>{genre}</td>
        <td>{numberInStock}</td>
        <td>{dailyRentalRate}</td>
        <td>
          <button onClick={this.handleDelete}>Delete</button>
        </td>
      </tr>
    );
  }
}

关于javascript - 需要 Reactjs 帮助来了解如何正确地将这个函数从父按钮组件传递到子按钮组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51730398/

相关文章:

scala - 如何转换 "pimp"方法来接受元组?

javascript - 等到前一个函数以 Angular 执行

javascript - HTML5 从一个 html 页面打开两个 web 套接字

javascript - 如何使用 promise 返回在 nodejs 的快速路由器中递归编辑的对象数组?

javascript - HTML/Javascript/jquery 函数按钮在 TextArea 中的每行末尾放置逗号

function - 使用rust 的嵌套动态功能对象

javascript - 如何操作 Twilio 数据对象? Node JS

javascript - SCSS 文件在使用 Webpack 使用react时未编译为 CSS

javascript - 无法使用服务器端渲染访问 DOM - react 0.14.1、react-dom 0.14.1 和 react-router 1.0.0-rc3

javascript - 在react-router-dom中使用条件渲染进行身份验证是否安全