【发布时间】:2019-08-12 14:18:39
【问题描述】:
我是 RN 的新手,我刚刚使用 react-native-elements 在我的目录页面中创建了一个搜索栏。但是,当我在搜索栏中输入角色的名字时,什么都不会触发。我认为我的状态设置正确,因为我遵循了文档。任何人都可以看看这个并阐明可能有什么问题吗?
这就是我设置搜索栏文件夹的方式。
//SearchHeader.js
import React from "react";
import {
Button,
View,
Text,
StyleSheet,
Platform,
Animated,
ScrollView,
ImageBackground,
TouchableOpacity
} from "react-native";
import {
createStackNavigator,
createAppContainer,
createBottomTabNavigator
} from "react-navigation";
import { SearchBar } from "react-native-elements";
// import MaterialCommunications from 'react-native-vector-icons';
export default class SearchHeader extends React.Component {
state = {
search: ""
};
updateSearch = search => {
this.setState({ search });
};
render() {
const { search } = this.state;
return (
<View style={styles.container}>
<SearchBar
inputStyle={{ backgroundColor: "white" }}
placeholder="Search for a character..."
placeholderTextColor={"#g5g5g5"}
onChangeText={this.updateSearch}
value={search}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
height: 50,
width: 390,
justifyContent: "center",
marginTop: 50
}
});
这就是我将它导入目录文件夹的方式:
//EmployeeDirectory.js
import React from "react";
import {
Button,
View,
Text,
StyleSheet,
Platform,
Animated,
ScrollView,
ImageBackground,
TouchableOpacity
} from "react-native";
import SearchHeader from './SearchHeader';
import { withNavigation } from "react-navigation";
import {
createStackNavigator,
createAppContainer,
createBottomTabNavigator
} from "react-navigation";
import { SearchBar } from "react-native-elements";
class EmployeeDirectory extends React.Component {
static navigationOptions = ({ navigation }) => ({
title: "Character Directory",
headerStyle: {
backgroundColor: "#53b4e6"
},
headerTintColor: "#f6c945",
headerTitleStyle: {
fontWeight: "bold"
}
});
render() {
return (
<View style={styles.container}>
<ImageBackground
source={{
uri:
"https://backgrounddownload.com/wp-content/uploads/2018/09/simpsons-clouds-background-5.jpg"
}}
style={{
width: "100%",
height: "100%",
alignContent: "center",
justifyContent: "center",
alignItems: "center"
}}
>
<SearchHeader />
<TouchableOpacity
onPress={() => this.props.navigation.navigate("HomerSimpson")}
style={styles.button}
>
<Text style={styles.text}>Homer Simpson</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => this.props.navigation.navigate("BartSimpson")}
style={styles.button}
>
<Text style={styles.text}>Bart Simpson</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => this.props.navigation.navigate("MargeSimpson")}
style={styles.button}
>
<Text style={styles.text}>Marge Simpson</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => this.props.navigation.navigate("LisaSimpson")}
style={styles.button}
>
<Text style={styles.text}>Lisa Simpson</Text>
</TouchableOpacity>
</ImageBackground>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
alignContent: "center"
},
text: {
fontSize: 25,
fontWeight: "bold",
color: "#f6c945",
alignItems: "center",
justifyContent: "center",
alignContent: "center"
},
button: {
flexDirection: "row",
backgroundColor: "#2d98da",
alignItems: "center",
justifyContent: "center",
alignContent: "center",
marginTop: 10,
marginBottom: 10,
borderRadius: 10,
borderWidth: 1.0,
borderColor: "black",
height: 30,
width: 260
}
});
export default withNavigation(EmployeeDirectory)
【问题讨论】:
-
你没有列表搜索返回的结果。
-
@hongdevelop 会放在搜索文件夹还是目录文件夹中?我猜这也会进入构造函数?
标签: react-native ecmascript-6 searchbar