一、变量解构一解就报错 优化前:
const App = (props) => {
const { data } = props;
const { name, age } = data
}
优化后:
const App = (props) => {
const { data } = props;
const { name, age } = data || {};
}
优化前
import React, { useState } from 'react';
const App = () => {
const [loading, setLoading] = useState(false);
const getData = async () => {
setLoading(true);
const res = await queryData();
setLoading(false);
}
}
优化后
import React, { useState } from 'react';
import to from 'await-to-js';
const App = () => {
const [loading, setLoading] = useState(false);
const getData = async () => {
setLoading(true);
const [err, res] = await to(queryData());
setLoading(false);
}
}