【问题标题】:Why is _this.state.searchAddress() not a function?为什么 _this.state.searchAddress() 不是函数?
【发布时间】:2018-11-05 00:10:53
【问题描述】:

我正在尝试使用 axios 在 react-native 中进行搜索查询,但是,当我尝试将其读取到控制台时出现此错误:

_this.state.searchAddress 不是函数。(在'`this.state.searchAddress()','_this.state.searchAddress' 是“数据”

我正在学习来自 C++ 的 react-native,所以我不完全理解这些错误。在addressSearch 上抛出错误。

class MainScreen extends Component {
  state = {
    searchAddress: "",
    addressData: {}
  };

  addressSearch = () => {
    console.log(this.state.searchAddress);
    Keyboard.dismiss();
    const addyZip = this.state.searchAddress();

    const query =
      "https://www.googleapis.com/civicinfo/v2/voterinfo?address= " +
      addyZip +
      "&electionId=6000&key=";

    axios.get(query).then(response => {
      console.log(response);
    });
  };

  render() {
    return (
      <Container>
        <SearchHeader
          value={this.state.searchAddress}
          onChangeText={searchAddress => this.setState({ searchAddress })}
          addressSearch={this.addressSearch}
        />
      </Container>
    );
  }
}

export default MainScreen;

【问题讨论】:

    标签: reactjs react-native axios


    【解决方案1】:

    变量this.state.searchAddress 是一个字符串,您在设置初始状态时最初将其设置为""。您试图将字符串调用为函数,但字符串不是函数。

    你的意思可能是:

    const addyZip = this.state.searchAddress;
    

    【讨论】:

      【解决方案2】:

      基本上在addressSearch函数中你需要改变

      const addyZip = this.state.searchAddress();

      const addyZip = this.state.searchAddress;

      因为 this.state.searchAddress 是一个字符串而不是一个函数。

      【讨论】:

        猜你喜欢
        • 2021-09-09
        • 2019-10-12
        • 2019-05-25
        • 2021-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多