【发布时间】:2019-02-24 18:36:45
【问题描述】:
目标是允许用户在搜索栏中输入关键字,将搜索词或短语存储到字符串中,然后向电影服务器发送发布请求,并以 FlatList 格式显示结果。
我不精通 javascript,但到目前为止,我能够将搜索输入存储到一个变量中,并通过控制台记录搜索来确认它,但使用该变量来呈现和显示令人困惑的结果
import React, { Component } from "react";
import {
View,
Text,
FlatList,
StyleSheet
} from "react-native";
import { Container, Header,Item,Input, Left, Body, Right, Button, Icon,
Title } from 'native-base';
class Search extends Component {
constructor(props) {
super(props);
this.state = {text: ''};
this.state = {
dataSource: []
}
}
renderItem = ({item}) => {
return (
<Text>{item.title}</Text>
)}
componentDidMount() {
const apikey = "&apikey=thewdb"
const url = "http://www.omdbapi.com/?s="
fetch(url + this.state.text + url)
.then((response) => response.json())
.then((responseJson)=> {
this.setState({
dataSource: responseJson.Search
})
})
.catch((error) => {
console.log(error)
})
}
render() {
return (
<Container>
<Header
searchBar rounded
>
<Item>
<Icon name="ios-search" />
<Input
placeholder="Type here to translate!"
onChangeText={(text) => this.setState({text})}
/>
</Item>
<Button
transparent
onPress={()=> {
{console.log(this.state.text)}
}
}
>
<Text>Search</Text>
</Button>
</Header>
<FlatList
style={{flex: 1, width:300}}
data={this.state.dataSource}
keyExtractor={(item, index) => 'key'+index}
renderItem={this.renderItem}
/>
</Container>
);
}
}
export default Search;
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center'
}
});
我的代码有点草率,所以请原谅我,我还是新手。
【问题讨论】:
-
如果你在snack.expo.io中添加一个工作副本会很棒
-
好的,我会的。 :)
标签: react-native react-native-android react-navigation react-native-ios native-base