相关疑难解决方法(0)

React - uncaught TypeError:无法读取undefined的属性'setState'

我收到以下错误

未捕获的TypeError:无法读取未定义的属性"setState"

甚至在构造函数中绑定delta之后.

class Counter extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            count : 1
        };

        this.delta.bind(this);
    }

    delta() {
        this.setState({
            count : this.state.count++
        });
    }

    render() {
        return (
            <div>
                <h1>{this.state.count}</h1>
                <button onClick={this.delta}>+</button>
            </div>
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

284
推荐指数
12
解决办法
22万
查看次数

反应这是未定义的

this承诺中的React 未定义.这是我的代码:

export default class Album extends React.Component {

    constructor(props) {
        super(props);
    }

    componentDidMount ()  {
        console.log(this.props.route.appState.tracks);  // `this` is working
        axios({
            method: 'get',
            url: '/api/album/' + this.props.params.id + '/' + 'tracks/',
            headers: {
                'Authorization': 'JWT ' + sessionStorage.getItem('token')
            }
        }).then(function (response) {
            console.log(response.data);
            this.props.route.appState.tracks.concat(response.data); // 'this' isn't working
        }).catch(function (response) {
            console.error(response);
            //sweetAlert("Oops!", response.data, "error");
        })
    }
Run Code Online (Sandbox Code Playgroud)

这是错误代码:

TypeError: this is undefined
Stack trace:
componentDidMount/<@webpack:///./static/apps/components/Album.jsx?:81:17
Run Code Online (Sandbox Code Playgroud)

this reactjs

6
推荐指数
1
解决办法
8635
查看次数

标签 统计

reactjs ×2

javascript ×1

this ×1