V
V
Viktor2020-06-09 14:14:44
React
Viktor, 2020-06-09 14:14:44

Why doesn't componentDidCatch work?

Made the ErrorBoundary component wrap the application

import React, { Component } from 'react'

class ErrorBoundary extends Component {
    state = {
        hasError: false
    }

    componentDidCatch(error, info) {
        console.log(error)
        console.log(info)
        this.setState({
            hasError: true
        })
    }

    render () {
        if(this.state.hasError) {
            return <h1>Какая-то ошибка</h1>
        }
        return this.props.children
    }
}

export default ErrorBoundary


When mounting one of the components, a request is made to the server
export const postVolonteerList = async (page = 1, perPage = 10, filters) => {
    try {
        let res = await instance
            .get(
                `/api/voluinteers`,
                {
                    params: {
                        page: page,
                        page_size: perPage,
                        ...filters
                    }
                });
        return res.data
    }catch(err){
        console.log('ОШИБКА', err.message);
        throw err
    }
};


I deliberately made an invalid request and throw an error in catch, but ErrorBoundary does not catch it. What could be the reason

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Didenko, 2020-06-09
@polyak-888

componentDidCatch is triggered when an error occurs during rendering, or when an error occurs when working with state

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question