在 React.js 中的 render return() 中显示获取结果

2022-01-20 00:00:00 arrays reactjs fetch javascript

我的问题是关于如何在 render return() 中显示数组结果.
我对 API 进行了提取,现在我得到了存储在数组中的结果.我需要显示此结果,但我尝试在返回中使用 for{},但它不起作用,我还尝试使用 .map 并且 map 未定义.

fetch(url + '/couch-model/?limit=10&offset=0', {方法:'GET',标题:{'内容类型':'应用程序/json','接受': '应用程序/json','授权':'JWT' + (JSON.parse(localStorage.getItem('token')).token)}}).then(res => {如果(解决){返回 res.json();} 别的 {抛出错误(res.statusText);}}).then(json => {这个.setState({模型:json.results}, () =>{/*console.log('modelosJSON: ', json);*/});})使成为() {常量 { isLoaded } = this.state;常量模型数组 = this.state.models;console.log('modelos: ', modelsArray);如果(!isLoaded){返回 (<div>加载中...</div>)} 别的 {返回 (

/*在这里显示结果*/</div>)}}

数组是这样的:

解决方案

模型数组是你的fetch返回的json的results,所以你可以设置将 models 设置为您的状态,并将 isLoaded 设置为 true 以便在加载模型时隐藏加载指示器.

示例

class App 扩展 React.Component {state = { isLoaded: false, 模型: [] };组件DidMount() {fetch(url + "/couch-model/?limit=10&offset=0", {方法:获取",标题:{内容类型":应用程序/json",接受:应用程序/json",授权:"JWT" + JSON.parse(localStorage.getItem("token")).token}}).then(res => {如果(解决){返回 res.json();} 别的 {抛出错误(res.statusText);}}).then(json => {这个.setState({模型:json.results,已加载:真});});}使成为() {常量 { isLoaded, 模型 } = this.state;如果(!isLoaded){返回 <div>加载中...</div>;}return <div>{models.map(model => <div key={model.id}>{model.code}</div>)}</div>;}}

My question is about how to show array results in render return().
I made a fetch to the API and now I get results that get stored in a array. I need to show this results but I tried with a for{} inside the return and it doesn't work, and I also tried with .map and the map is undefined.

fetch(url + '/couch-model/?limit=10&offset=0', {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Authorization': 'JWT ' + (JSON.parse(localStorage.getItem('token')).token)
        }
    }).then(res => {
        if (res.ok) {
            return res.json();
        } else {
            throw Error(res.statusText);
        }
    }).then(json => {
        this.setState({
             models: json.results
        }, () => {
            /*console.log('modelosJSON: ', json);*/
        });
    })

render() {
    const { isLoaded } = this.state;
    const modelsArray = this.state.models;

    console.log('modelos: ', modelsArray);

    if (!isLoaded) {
        return (
            <div>Loading...</div>
        )
    } else {

        return (
            <div>
                /*show results here*/
            </div>
        )
   }
}

The array is this:

解决方案

The array of models is the results of the json returned from your fetch, so you can set that as models in your state instead, and set isLoaded to true so the loading indicator is hidden when the models are loaded.

Example

class App extends React.Component {
  state = { isLoaded: false, models: [] };

  componentDidMount() {
    fetch(url + "/couch-model/?limit=10&offset=0", {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
        Accept: "application/json",
        Authorization: "JWT " + JSON.parse(localStorage.getItem("token")).token
      }
    })
      .then(res => {
        if (res.ok) {
          return res.json();
        } else {
          throw Error(res.statusText);
        }
      })
      .then(json => {
        this.setState({
          models: json.results,
          isLoaded: true
        });
      });
  }

  render() {
    const { isLoaded, models } = this.state;

    if (!isLoaded) {
      return <div>Loading...</div>;
    }

    return <div>{models.map(model => <div key={model.id}>{model.code}</div>)}</div>;
  }
}

相关文章