【问题标题】:How to render API response in react JS?如何在 React JS 中呈现 API 响应?
【发布时间】:2020-06-03 17:04:04
【问题描述】:

我是 React JS 的新手,我已经被一个问题困扰了很长一段时间。

所以,这是代码,

import React from 'react';

class SearchForm extends React.Component {

async handleSubmit(event){
      event.preventDefault();
try{
const url ='/jobs/all/'
const Response = await fetch((url),{
    method: `GET`,
    mode: 'cors',
        headers: { 
            'Accept': 'application/json'
          }});

const filtered = [];

const res = await Response.json();
const Location = this.refs.location.value;
const Category = this.refs.category.value; 

Object.keys( res ).forEach( function( key ) {
    if( res[key].location === Location && res[key].category === Category ) {
        filtered[key] = res[key];}
});
console.log(filtered)
        }
 catch (err) {
    console.error('err', err);}
    };
    render() {
      return (
        <div>
        <form action="/search" onSubmit={this.handleSubmit.bind(this)}>
          <select ref="category">
            <option value="" defaultValue>Category</option>
            <option value="Ios Developer">Ios Developer</option>
            <option value="Java Developer">Java Developer</option>
            <option value="Marketing">Marketing Generalist</option>
            <option value="Web Developer">Web Developer</option>
            <option value="Python Developer">Python Developer</option>
            <option value="C# Developer">C# Developer</option>
          </select>
          <select ref="location">
            <option value="" defaultValue>Location</option>
            <option value="Lucknow">Lucknow</option>
            <option value="Ranchi">Ranchi</option>
            <option value="Delhi">Delhi</option>
          </select>
          <button>Find</button>
        </form>
        </div>
      );
    }
  }

export default SearchForm;

这是我的回应,

Response

我想呈现响应。 还有一些情况会导致多个响应,我想渲染所有这些。

请帮帮我。

【问题讨论】:

    标签: javascript json reactjs api


    【解决方案1】:

    请尝试以下代码。

    import React from 'react';
    
    class SearchForm extends React.Component {
    state={
    filtered :[]
    }
    
    async handleSubmit(event){
          event.preventDefault();
    try{
    const url ='/jobs/all/'
    const Response = await fetch((url),{
        method: `GET`,
        mode: 'cors',
            headers: { 
                'Accept': 'application/json'
              }});
    
    const filtered = [];
    
    const res = await Response.json();
    const Location = this.refs.location.value;
    const Category = this.refs.category.value; 
    
    Object.keys( res ).forEach( function( key ) {
        if( res[key].location === Location && res[key].category === Category ) {
            filtered[key] = res[key];}
    });
    this.setState({filtered })
            }
     catch (err) {
        console.error('err', err);}
        };
        render() {
          return (
            <div>
            <form action="/search" onSubmit={this.handleSubmit.bind(this)}>
              <select ref="category">
                <option value="" defaultValue>Category</option>
                <option value="Ios Developer">Ios Developer</option>
                <option value="Java Developer">Java Developer</option>
                <option value="Marketing">Marketing Generalist</option>
                <option value="Web Developer">Web Developer</option>
                <option value="Python Developer">Python Developer</option>
                <option value="C# Developer">C# Developer</option>
              </select>
              <select ref="location">
                <option value="" defaultValue>Location</option>
                <option value="Lucknow">Lucknow</option>
                <option value="Ranchi">Ranchi</option>
                <option value="Delhi">Delhi</option>
              </select>
              <button>Find</button>
            </form>
            {this.state.filtered.map((data)=>{
               return <div>{data.location}</div>  // you can render here list items
            })}
            </div>
          );
        }
      }
    
    export default SearchForm;
    

    【讨论】:

    • 警告:列表中的每个孩子都应该有一个唯一的“key”道具。这个可以忽略吧?
    • 不,不要忽略键。任何时候创建列表时,都需要添加键,以便 react 可以正确更新 DOM。只需将 key={data.id} 添加到您的地图中。(只要您的 ID 是唯一的)。示例:返回
      {data.location}
    • 好的,可以了!谢谢 vipin 和 @Sir Codes Alot!
    猜你喜欢
    • 2020-11-08
    • 2023-04-01
    • 2011-05-19
    • 2022-12-29
    • 2021-09-23
    • 2021-02-25
    • 1970-01-01
    • 2020-11-05
    • 2019-06-28
    相关资源
    最近更新 更多