小编hel*_*pie的帖子

在 ReactJS 中使用搜索框时如何显示来自 API 的信息?

我正在使用 Star Wars API 构建一个 React JS 项目。我的应用程序的目标是能够搜索字符。

这是我的应用程序中搜索组件的代码。

目前,我可以通过 API 检索数据并在页面上显示信息,但我无法弄清楚在搜索时如何显示这些信息。

有任何想法吗?

import React, { Component } from 'react';
class Search extends Component {
  constructor(props){
    super(props)
    this.state = {
      query:'',
      peoples: [],
    }
  }

 onChange (e){
   this.setState({
     query: e.target.value
   })
  if(this.state.query && this.state.query.length > 1) {
     if(this.state.query.length % 2 === 0){
       this.componentDidMount()
     }
   }
 }

componentDidMount(){
  const url = "https://swapi.co/api/people/";
  fetch (url,{
    method:'GET'
  }).then(results => {
    return results.json();
  }).then(data => {
    let peoples = data.results.map((people) => {
      return(
        <ul …
Run Code Online (Sandbox Code Playgroud)

javascript api search reactjs

5
推荐指数
1
解决办法
1万
查看次数

连接字符串和 &lt;a href=""&gt;&lt;/a&gt;

我正在使用 ReactJS,我想要一个字符串,然后是一个链接,如下所示

const example = "Hello I'm a string" + <a href="/link">And this is a link</a>
Run Code Online (Sandbox Code Playgroud)

目前我不断得到 Hello I'm a string [object Object]

如何让文本和链接正确连接?

javascript string string-concatenation reactjs

1
推荐指数
1
解决办法
594
查看次数

标签 统计

javascript ×2

reactjs ×2

api ×1

search ×1

string ×1

string-concatenation ×1