首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React从URL获取参数

问React从URL获取参数
EN

Stack Overflow用户
提问于 2019-12-14 11:23:50
回答 1查看 41关注 0票数 0

我是个新手,已经用了几周了。我想知道如何让我的cod动态地从url获取数据并显示它。

代码语言:javascript
复制
   searchSome() {
var serachValue=
  "http://localhost:8005/api?act=search&term=" + this.state.valSearch+ "+";
fetch(serachValue)
  .then(data => data.json())
  .then(result => {
    // console.log(this.state.library);

    var object= JSON.stringify(this.state.library);
    var stringify = JSON.parse(object);
  });
}

链接结果的数据,如id,姓名等。我需要过滤,因为用户在搜索栏中输入的词,并显示它。

显示代码

代码语言:javascript
复制
const filter = this.state.library.filter(book=> {
  return (
    book.name.indexOf(this.searchSome()) !== -1 ||
    book.description
      .indexOf(this.searchSome()) !== -1
  );
});

任何帮助都非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2019-12-14 12:01:00

请在下面尝试

代码语言:javascript
复制
searchSome(searchTerm) {
    var serachValue=
      "http://localhost:8005/api?act=search&term=" + searchTerm + "+";
    fetch(serachValue)
      .then(data => data.json())
      .then(result => {
        // Not sure what is your format, but you have to set this data to state
        this.setState({ library: result });
    }); 
}

render() {
    return (
        <View>
            <TextInput
                onChangeText={(text) => {
                    // Should debounce the call to `searchSome` to avoid multiple request
                    this.searchSome(text);
                    this.setState({ searchValue: text })
                }}
                text={this.state.searchValue}
            />
            <FlatList 
                data={this.state.library}
            />
        </View>
    )
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59332058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档