如何在React js中的两个不同函数中设置状态

我正在使用React js。我有两个不同的功能。

第一:

componentDidMount() {
    fetch("http://131.181.190.87:3001/all")
      .then((res) => res.json())
      .then((json) => {
        this.setState({
          isLoaded: true,
          symbolsname: json,
          items: json,
        });
      });
  }

第二:

 handleClick(symbol) {
    fetch(`http://131.181.190.87:3001/history?symbol=${symbol}`)
      .then((res) => res.json())
      .then((json) => {
        this.updateStateOfSymbols(json);
        this.setState({
          isLoad: true,
          symbolsname: json,
        });
      });
  }

The first function get called first once the server runs. There is a table that loads on the page when the server runs and it has some clickable table data or the links. When the user click on those link the second function get called. But, the setState does not work for second function. I am not getting any error beside it just says its undefined when I try to print it. But, if I pass the value to symbolsname in first function it does shows the output. I am assuming you cannot use setState two times in different function. Is there any way to make it work?

任何帮助表示赞赏。谢谢

评论