【问题标题】:Undefined setState in ReactReact 中未定义的 setState
【发布时间】:2020-04-02 02:18:00
【问题描述】:

我尝试在前面使用 API 中显示数据,但在浏览器控制台中出现此错误状态并且数据未显示:

Uncaught (in promise) TypeError: Cannot read property 'setState' of undefined 在 eval (tickets.jsx:190)

这就是我在 render() 中显示数据的方式:


 render(){
        var { items_tickets, dd_tickets, role, filter_tickets, all_tickets } = this.state;
        return(
 <table className="table table-striped table-dark table-hover">
                   <thead>
                       <tr>
                            <th> N° de Ticket </th> 
                            <th> Mes Gains  </th> 
                            <th> (Non) utilisé  </th> 
                       </tr>
                   </thead>
                   <tbody>
{
filter_tickets.length > 0 ? filter_tickets.map(item => {
                                console.log('filter tickets : ', filter_tickets)
                            const {numTicket, libelleGain, used} = item;
                            // console.log("items_tickets : ", items_tickets)
                            return  <tr key={numTicket}> 
                            <td>{numTicket}</td>
                                <td>{libelleGain}</td>
                                <td>{used === true ? "Utilisé" : "Non utilisé"}</td>
                                    </tr>
                            }) : "d"
                       } 
  </tbody>
                </table>
            </div>
        </div>
        )
    }

这是我前面的API函数:

    // Get all tickets 
    filterallTickets(){
        const head = localStorage.getItem('head'); 
        const payload = localStorage.getItem('payload');
        const signature = localStorage.getItem('signature');
        var tok = head + "." + payload + "." + signature;
 // Gains List
        fetch(`${API}/api/tickets/getalltickets`,{

            method: 'GET',
             headers :{
                'Content-Type': 'application/json',
                'authorization': `Bearer ${tok}`, } })
                .then(results => {
                    return results.json();
                })
                .then(res => {
                        console.log("res : ",res);
                    if(res.success === true){
                        this.setState({ filter_tickets: res.result });
                        // this.setState({items_tickets: []});
                    }
                })
    }

谢谢

【问题讨论】:

    标签: reactjs fetch state render setstate


    【解决方案1】:

    方法一(简单)

    更改您的声明:

    filterallTickets () {
    

    到箭头函数:

    filterallTickets = () => {
    

    方法二

    在您的构造函数中,将您的函数绑定到this

    constructor() {
      this.filterallTickets = this.filterallTickets.bind(this);
    }
    

    【讨论】:

    • 这很奇怪,我有另一个 API 函数:filterTickets(),它工作正常!为什么要使用箭头函数?
    • @mitsu 如果未绑定thisthis 的上下文将变为未定义。
    • 但我已经有了:this.filterallTickets = this.filterallTickets.bind(this);我只是没有把它放在帖子中,它仍在使用不是箭头功能的 filterTickets() !
    • 是的,它适用于此,但仍然不明白如何,即使我有同样的无箭头功能,它也适用! ://
    猜你喜欢
    • 1970-01-01
    • 2019-05-22
    • 2019-06-13
    • 2019-04-09
    • 2018-12-05
    • 2016-03-03
    • 2020-07-10
    • 2021-01-24
    • 2019-08-27
    相关资源
    最近更新 更多