touchableopacity onpress 函数未定义(不是函数)React Native
touchableopacity onpress function undefined (is not a function) React Native
我希望在点击 TouchableOpacity 按钮后能够导航到新屏幕,但我收到一条错误消息
_this3.handleThisTap is not a function. (In '_this3.handleThisTap()','_this3.handleThisTap' is undefined)
import React, { Component } from 'react';
import {
Text,
StyleSheet,
View,
TextInput,
KeyboardAvoidingView,
FlatList,
TouchableOpacity,
TouchableHighlight,
} from 'react-native';
import {
SearchBar,
Wrapper,
ItemWrapper,
} from './searchView.styles';
export default class SearchView extends Component {
constructor(props) {
super(props);
this.state = {
feedUrl: 'https://api.urbandictionary.com/v0/autocomplete?term=',
isLoading: true,
}
}
handleTextChange(text) {
let url = this.state.feedUrl + text;
return fetch(url)
.then((response) => response.json())
.then((res) => {
this.setState({
data: res,
isLoading: false,
})
})
.catch((error) => {
console.log(error);
})
}
handleThisTap(item) {
this.props.navigation.navigate('FeedView', item);
}
_renderItem({ item }) {
return (
<ItemWrapper
underlayColor='white'
onPress={() => this.handleThisTap(item)}>
<Text>{item}</Text>
</ItemWrapper>
)
}
render() {
return (
<Wrapper behavior="padding">
<SearchBar
style={{
shadowOffset: {
width: 0,
height: 5,
},
}}
autoFocus={true}
clearTextOnFocus={true}
placeholder="Search for text here"
returnKeyType='search'
clearButtonMode='always'
keyboardShouldPersistTaps={true}
onChangeText={(text) =>
this.handleTextChange(text)
} />
<FlatList
data={this.state.data}
renderItem={this._renderItem}
/>
</Wrapper>
)
}
}
我试过使用bind.(this)
感谢任何帮助。
错误是因为您没有将 _renderItem
绑定到 this
。绑定在constructor
:
constructor(props) {
super(props);
this.state = {
feedUrl: 'https://api.urbandictionary.com/v0/autocomplete?term=',
isLoading: true,
}
this._renderItem = this._renderItem.bind(this); //add this line
}
我希望在点击 TouchableOpacity 按钮后能够导航到新屏幕,但我收到一条错误消息
_this3.handleThisTap is not a function. (In '_this3.handleThisTap()','_this3.handleThisTap' is undefined)
import React, { Component } from 'react';
import {
Text,
StyleSheet,
View,
TextInput,
KeyboardAvoidingView,
FlatList,
TouchableOpacity,
TouchableHighlight,
} from 'react-native';
import {
SearchBar,
Wrapper,
ItemWrapper,
} from './searchView.styles';
export default class SearchView extends Component {
constructor(props) {
super(props);
this.state = {
feedUrl: 'https://api.urbandictionary.com/v0/autocomplete?term=',
isLoading: true,
}
}
handleTextChange(text) {
let url = this.state.feedUrl + text;
return fetch(url)
.then((response) => response.json())
.then((res) => {
this.setState({
data: res,
isLoading: false,
})
})
.catch((error) => {
console.log(error);
})
}
handleThisTap(item) {
this.props.navigation.navigate('FeedView', item);
}
_renderItem({ item }) {
return (
<ItemWrapper
underlayColor='white'
onPress={() => this.handleThisTap(item)}>
<Text>{item}</Text>
</ItemWrapper>
)
}
render() {
return (
<Wrapper behavior="padding">
<SearchBar
style={{
shadowOffset: {
width: 0,
height: 5,
},
}}
autoFocus={true}
clearTextOnFocus={true}
placeholder="Search for text here"
returnKeyType='search'
clearButtonMode='always'
keyboardShouldPersistTaps={true}
onChangeText={(text) =>
this.handleTextChange(text)
} />
<FlatList
data={this.state.data}
renderItem={this._renderItem}
/>
</Wrapper>
)
}
}
我试过使用bind.(this)
感谢任何帮助。
错误是因为您没有将 _renderItem
绑定到 this
。绑定在constructor
:
constructor(props) {
super(props);
this.state = {
feedUrl: 'https://api.urbandictionary.com/v0/autocomplete?term=',
isLoading: true,
}
this._renderItem = this._renderItem.bind(this); //add this line
}