代码之家  ›  专栏  ›  技术社区  ›  Krzysztof Durak

过滤React js[重复]

  •  1
  • Krzysztof Durak  · 技术社区  · 7 年前

    我需要简单筛选待办事项列表的帮助我已经成功地删除和添加任务,效果很好,但现在我正在尝试筛选添加的任务,我收到了错误消息我完全是初学者,所以请记住这一点谢谢!

    TypeError:无法读取未定义的属性“setState”

    26 |这个。设置状态({查询:evt.target.value});

    import React, {Component} from 'react';
    import '../App.css';
    import  Form from './Form';
    import  List from './List';
    
    class App extends Component {
    
    state = {
        query: '',
        inputValue: "",
        todos: [
            {value: 'Naumiej  się Reacta', done: false},
            {value: 'Pucuj trzewiki ', done: true},
        ]
    }
    handleChange = (evt) => {
        this.setState({inputValue: evt.target.value});
    }
    removeMe = (index) =>{
        this.setState({
            todos: this.state.todos.filter((_, i) => i !== index)
        })
    }
    
    searchChanged(evt) {
        this.setState({query: evt.target.value});
    }
    
    handleSubmit = (evt) => {
        evt.preventDefault();
        const newTodo = {
            value: this.state.inputValue
        };
        const todos = this.state.todos;
        todos.push(newTodo);
        this.setState({todos: todos, inputValue: ''})
    
    }
    
    render() {
        return (
            <div className="App">
                <input type="text" placeholder="Search..." onChange=
    {this.searchChanged} />
                <Form
                    handleChange={this.handleChange}
                    inputValue={this.state.inputValue}
                    handleSubmit={this.handleSubmit}
    
                />
                <List
                    removeMe={this.removeMe}
                    todos={this.state.todos}
                    query={this.state.query}
                />
            </div>
        );
    }
    }
    
    export default App;
    

    列表

    import React, {Component} from 'react';
    import  Task from './Task';
    class List extends Component {
    
    render() {
        let serchedTasks = this.props.todos.filter(
            (todos) => {
                return todos.value.indexOf(this.props.query) !== -1;
            }
        );
        return (
            <div className="List">
                {serchedTasks.map((todo, index) => {
                    return (
                        <Task
                            key={index}
                            index={index}
                            removeMe={this.props.removeMe}
                            todo={todo}
                        />
                    )
                })}
    
            </div>
        )
    }
    }
    export default List;
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Arman Charan    7 年前

    searchChanged(evt) 不一定要 this .

    将其重新定义为箭头函数。

    searchChanged = (evt) => {
      this.setState({query: evt.target.value})
    }
    
        2
  •  0
  •   Dyo    7 年前

    像处理其他函数一样使用箭头函数:

    searchChanged = (evt) => {