React component error (error) collection method

React component error (error) collection method

1. Add error state variables

	在state中添加error变量,初始化为null。
	this.state = { error : null }

2. Combine catch and setState() to capture the error object and save it in the state

	fetch( url ).then( result => console.log( result ) ).catch(e => this.setState({error : e}))

3. When an error occurs, you can get the error object in the state in the render() method, and use conditional rendering to display the error message

	if (error) { return <p>Something went wrong.</p> }

Guess you like

Origin blog.csdn.net/sjs1995/article/details/107949344