【问题标题】:React - onChange function 'this.state' is undefinedReact - onChange 函数“this.state”未定义
【发布时间】:2018-04-19 22:16:03
【问题描述】:

我正在尝试使用 React,并且正在尝试创建一个搜索来过滤项目列表。我有两个组件,主要的一个显示调用 Search 组件的项目列表。

我有一个onChange 函数,它将状态中的term 设置为输入值,然后从主组件调用searchItems 以过滤项目列表。出于某种原因,在searchItems 中,this.state 是未定义的。我认为在 Search 组件中将 bind 添加到 onInputChange 可以解决问题,但没有任何区别。也许我缺少一些东西。

主要组件

import React, { Component } from 'react';
import _ from 'lodash';

import Search from './search';

class Items extends Component {
    constructor(props) {
        super(props);
        this.state = {
            error: null,
            isLoaded: false,
            items: []
        };
    }

    componentDidMount() {
        fetch("[url].json")
            .then(res => res.json())
            .then(
                (result) => {
                    this.setState({
                        isLoaded: true,
                        items: result
                    });
                }
            ),
            (error) => {
                this.setState({
                    isLoaded: true,
                    error
                })
            }
    }

    searchItems(term) {
        const { items } = this.state;
        const filtered = _.filter(items, function(item) {
            return item.Name.indexOf(term) > -1;
        });

        this.setState({ items: filtered });
    }

    render() {
        const { error, isLoaded, items } = this.state;

        if (error) {
            return <div>Error: {error.message}</div>;
        }
        else if (!isLoaded) {
            return <div>Loading...</div>;
        }
        else {
            return (
                <div>
                    <Search onSearch={this.searchItems}/>
                    <ul>
                        {items.map(item => (
                            <li key={item.GameId}>
                                {item.Name}
                            </li>
                        ))}
                    </ul>
                </div>
            )
        }
    }
}

export default Items;

搜索组件

import React, { Component } from 'react';

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

        this.state = {
            term: ''
        };
    }

    render() {
        return (
            <div>
                <input type="text" placeholder="Search" value={this.state.term} onChange={event => this.onInputChange(event.target.value)} />
            </div>
        );
    }

    onInputChange(term) {
        this.setState({ term });
        this.props.onSearch(term);
    }
}

export default Search;

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    您没有在Items 组件中绑定searchItems()

    尝试将其更改为箭头函数:

    searchItems = () => {
      // blah
    }
    

    或以其他方式将其绑定在constructor()

    constructor() {
      // blah
      this.searchItems = this.searchItems.bind(this);
    }
    

    或者当你调用它时。

    你可以阅读更多关于thishere的信息。

    【讨论】:

    • 啊,是的,成功了!老实说,我对 React 还很陌生,我是否正确地说,用于某些事件(例如 onChange)或更新的任何函数都需要绑定到访问 this
    • 一般来说,是的。你可以阅读更多关于here的信息。
    猜你喜欢
    • 2018-05-12
    • 2019-05-29
    • 2012-09-19
    • 1970-01-01
    • 2022-08-05
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多