简体   繁体   English

如何在按钮单击时使用 useEffect 行为

[英]How to use useeffect behavior on button click

So, basically I have a form that receives a phone number and when clicking the button it displays data from api that receives the phone number as a parameter to filter the data.所以,基本上我有一个接收电话号码的表单,当单击按钮时,它会显示来自 api 的数据,该数据接收电话号码作为过滤数据的参数。 At the same time I want that when clicking the button it takes the user to another dinamic link containing their code taken from the api, sth like 'user/001'.同时,我希望当单击按钮时,它会将用户带到另一个动态链接,其中包含从 api 获取的代码,例如“user/001”。 The thing is when i click the button i get the api data but the code in the link is undefined 'user/undefined' I think it can be for the fact that the link shows up before the api fetch itself but I do not know how can i deal with this situation.问题是当我单击按钮时,我得到 api 数据,但链接中的代码未定义“用户/未定义”我认为这可能是因为链接在 api 获取之前出现,但我不知道如何我可以处理这种情况吗? Also, after some serious debugging i realized that nothing is being stored in the state, when I use useEffect instead of the function handleClick im able to see the data in the state but with handleClick is the opposite.此外,经过一些认真的调试后,我意识到 state 中没有存储任何内容,当我使用 useEffect 而不是 function handleClick 时,我能够看到 Z9ED39E2EA931586B73A985A6942EZEF 中的数据相反但5。 Is it possible to create similar useEffect behavior to handle button event?是否可以创建类似的 useEffect 行为来处理按钮事件?

...... ……


  const [state, setState] = useState({});

  let headers = new Headers();
  headers.set("Authorization", "Basic " + btoa(username + ":" + password));

  const handleClick=() => {
    getData();
  };

  const getData = async () => {
    const a = await fetch(url, { method: "GET", headers: headers });
    const response = await a.json();
    setState(response.data[0]);
    console.log(response);
  };

  return (
    <div>
        <div>
          <p>{test.place}</p>
           <Link to={{pathname: `/person/${state.value}` }}> 
            <Button
              variant="contained"
              color="primary"
              className={cx(styles.button)}
              onClick={handleClick}
            >
              Entrar
            </Button> 
            </Link>
        </div>
    
    </div>
  );
};

export default FetchApi;

The problem is that the to props of the <Link> does not wait for the fetch to end before redirecting.问题是<Link>to道具在重定向之前不会等待fetch结束。

You should use the push method from the router.您应该使用路由器的push方法。

Assuming you are using React Router:假设您使用的是 React 路由器:

  const history = useHistory();

  const handleClick=() => {
    getData();
  };

  const getData = async () => {
    const response = await fetch(url, { method: "GET", headers: headers });
    const { data } = await response.json();

    setState(data[0]);
    
    history.push(`/person/${data[0].value}`)
  };

  return (
    <div>
        <div>
          <p>{test.place}</p>
            <Button variant="contained" color="primary" className={cx(styles.button)} onClick= {handleClick} >
              Entrar
            </Button>
        </div>
    </div>
  );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM