我正在尝试使用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;
答案 0 :(得分:3)
变量this.state.searchAddress
是一个字符串,您在设置初始状态时将其初始设置为""
。您正在尝试将字符串作为函数调用,但是字符串不是函数。
您可能是说:
const addyZip = this.state.searchAddress;
答案 1 :(得分:0)
基本上在addressSearch函数中,您需要更改
const addyZip = this.state.searchAddress();
到
const addyZip = this.state.searchAddress;
因为this.state.searchAddress是字符串而不是函数。