为什么_this.state.searchAddress()不是函数?

问题描述 投票:0回答:2

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

_this.state.searchAddress不是函数。(In'`this.state.searchAddress()','_ this.state.searchAddress'是“data”

我正在学习来自C ++的本地反应,所以我不完全理解这些错误。 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
2个回答
3
投票

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

你可能意味着:

const addyZip = this.state.searchAddress;

0
投票

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

const addyZip = this.state.searchAddress();

const addyZip = this.state.searchAddress;

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

© www.soinside.com 2019 - 2024. All rights reserved.