语法错误:以下代码有什么问题

时间:2019-02-15 09:44:04

标签: javascript react-native asynchronous-javascript

帮我解决以下显示语法错误的代码。我知道肯定有问题。如何借助箭头功能编写以下checkInventory订单。

class ClassName extends React.PureComponent {
    _ismounted = false;
    constructor(props) {
        super(props);
        this.default_item_ = {
            name: 'Items',
            index: 0,
            children: [],
        };
        this.state = {
            items: [],
        };
    }

    componentDidMount() {
        this._ismounted = true;
        this.load_items();
    }

    componentDidUpdate() {
        this._ismounted = true;
        this.load_items();
    }

    componentWillUnmount() {
        this._ismounted = false;
        this.unlisten_path_change();
    }

    load_items = () => {
        const file_name = 'file_name.json';
        client.get_file(this.props.item_id, file_name, 'json')
            .then((request) => {
                const items = request.response;
                this.default_root_item.children = items;
                if (this._ismounted) {
                    this.setState({ items: [this.default_root_item] });
                }
                this.handle_path_change(this.props.location.pathname);
            })
    };
}


handle_path_change = (new_path) => {
    if (new_path === this.last_location_pathname) {      
        return;
    }
    this.last_location_pathname = new_path;

    if (new_path.indexOf('/items/') < 0) {
        if (this.state.items_expanded) {
            this.handle_item_close();
        }
        return;
    }
    let item_id = parseInt(new_path.replace(/^.*\/items\//, 
    '').replace(/\D*$/, ''));
    if (isNaN(item_id) || item_id < 0) {
        item_id = 0;
    }
    const item = ClassName.find_item_recursively(this.state.items, 
    item_id);
    this.handle_root_item_change(item);
};

1 个答案:

答案 0 :(得分:0)

该代码是codecademy的教程的一部分 从而checkInventory()返回一个诺言并附加一个.then() 并且两个处理程序,即handleSuccess()handleFailure()作为参数传递。因此,代码将如下所示:

checkInventory(order).then(handleSuccess, handleFailure);