我正在尝试使用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;
变量this.state.searchAddress
是一个字符串,当你设置初始状态时,它最初设置为""
。您正在尝试将字符串作为函数调用,但字符串不是函数。
你可能意味着:
const addyZip = this.state.searchAddress;
基本上在addressSearch函数中你需要改变
const addyZip = this.state.searchAddress();
至
const addyZip = this.state.searchAddress;
因为this.state.searchAddress是一个字符串而不是一个函数。