简体   繁体   English

通过反应组件传递索引

[英]passing index through react components

I'm studying Reactjs and I'm building a tasks project (CRUD) but I'm stuck at the point of editing, the editing part is in another component and I'm not able to send the index of the task that will be edit, I read the documentation but I'm not capable to make it, please if someone can see my code and tell what I'm doing wrong. 我正在研究Reactjs,正在构建任务项目(CRUD),但是我停留在编辑时,编辑部分在另一个组件中,并且我无法发送将要执行的任务的索引编辑,我阅读了文档,但是我做不到,如果有人可以看到我的代码并告诉我我做错了,请。

the app (main)code 应用程序(主)代码

    import React, { Component } from 'react';
    import logo from './logo.svg';
    import './App.css';

    // data
    import { todos2 } from './todos.json';

    // subcomponents
    import TodoForm from './components/TodoForm';
    import TodoFormEdit from './components/TodoFormEdit';

    class App extends Component {
      constructor() {
        super();
        this.state = {
          todos2, mode:'view'
        }

        this.handleAddTodo = this.handleAddTodo.bind(this);
        this.handleEdit2 = this.handleEdit2.bind(this);
      }

      removeTodo(index) {
        this.setState({
          todos2: this.state.todos2.filter((e, i) => {
            return i !== index
          })
        }); 
      }

      handleAddTodo(todo) {
        this.setState({
          todos2: [...this.state.todos2, todo]
        })
      }


      handleEdit2(i) {
        this.setState({mode: 'edit'});
        //const mode = mode === 'edit';


        alert(i);

         /* alert(this.state.todos2[i].title);
          alert(this.state.todos2[i].priority);
          alert(this.state.todos2[i].description);
          alert(this.state.todos2[i].language);*/


      }

      render() {

        const todosAll = this.state.todos2.map((todo, i) => {
          return (
            <div className="col-md-4" key={i}>
              <div className="card mt-4">
                <div className="card-title text-center">
                  <h3>{todo.title} - { i } </h3>
                  <span className="badge badge-pill badge-danger ml-2">
                    {todo.priority}
                  </span>
                </div>
                <div className="card-body">
                  <div>
                    {todo.description}
                  </div>
                  <div>
                    {todo.language}
                  </div>
                </div>
                <div className="card-footer">
                  <button
                    className="btn btn-danger"
                    onClick={this.removeTodo.bind(this, i)}>
                    Delete
                  </button>
                  <button
                    className="btn btn-warning ml-2"
                    onClick={this.handleEdit2.bind(this, i)}>
                    Edit
                  </button>
                </div>
              </div>
            </div>
          )
        });

return (
      <div className="App">

        <nav className="navbar navbar-dark bg-dark">
          <a className="navbar-brand" href="/">
            Tasks
            <span className="badge badge-pill badge-light ml-2">
              {this.state.todos2.length} 
            </span>
          </a>
        </nav>

        <div className="container">
          <div className="row mt-4">

            <div className="col-md-4 text-center">
                <img src={logo} className="App-logo" alt="logo" />
                {/* <TodoForm onAddTodo={this.handleAddTodo} ></TodoForm> */ }
                {this.state.mode === 'view' ? (
                   <TodoForm onAddTodo={this.handleAddTodo} />
                ) : (
                  <TodoFormEdit index={this.state.i}/>
                )}
            </div>

            <div className="col-md-8">
              <div className="row">
                {todosAll}
              </div>
            </div>


          </div>
        </div>


      </div>
    )

  }
}

export default App;

and the Edit component: 和编辑组件:

    import React, { Component } from 'react';

// data
import { todos2 } from '../todos.json';

class TodoFormEdit extends Component {

  constructor (i) {
    super(i);
    this.state = {
      todos2
    };



  }

  render() {



      return (

        <div>
          {this.state.todos2[0].title}
        </div>

      )



  }

}

export default TodoFormEdit;

You're passing this.state.i : 您正在通过this.state.i

<TodoFormEdit index={this.state.i}/>

It's not clear where you set it–I see mode and todos2 state properties, I don't see i anywhere. 目前尚不清楚您在哪里设置它-我看到modetodos2状态属性,在任何地方都看不到i

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

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