useEffect挂钩后状态未更新

时间:2019-11-14 03:10:23

标签: javascript reactjs react-hooks use-effect

我需要从length获取列表的API并将其显示为计数器。 但是加载后,它又回到了原始状态。

let dataAll = []; //Global variable

const [isLoading, setIsLoading] = useState(false);
const [isAll, setIsAll] = useState(dataAll);
const [state, setState] = useState({
    allCounter: 0,
    loading: true,
    });

const fetchAll = async (params = {}) => {
    try {
        dataAll = await TriggerRequestAPI.getAllTickets();
        const counter = dataAll.length;

        setIsLoading(false);
        setIsAll(dataAll);
        setState({
            state,
            loading: false,
            allCounter: counter,
            params
        });
    }
catch (err) {
    setIsLoading(true);
    console.log(err);
    message.error(`There is a problem in listing all table`, 3);
    }
};

useEffect(() => {
    setIsLoading(true);
    fetchAll();
    }, []);

0 个答案:

没有答案