为什么setState不更改值?

时间:2020-10-31 06:32:52

标签: reactjs

为什么下面的代码不起作用? 即使我将reverse设置为true,它也会返回false。 它还会记录命中率,因此可以到达。

import React, { useEffect, useState } from 'react';
import { ComponentProps } from 'react';

import { useHistory } from 'react-router-dom';

type Props = {
} & ComponentProps<'div'>;

export function HeaderMaster({
  ...props
}: Props) {
  const [inverted, setInverted] = useState(false);
  const history = useHistory() 

   useEffect(() => {
    setInverted(true); // this does work
      history.listen((location) => { 
        console.log(location.pathname);
         if (location.pathname === '/bestellen') {
           setInverted(true); // this does not
           console.log('hit');
         }
         else {
          setInverted(false);
         }
      }) 

   },[history]);

   useEffect(() => {
    console.log(inverted);
  },[inverted]) 

  return (
    <>
    </>
  );
}

0 个答案:

没有答案