sách gpt4 ai đã đi

javascript - 进行 fetch 调用时出错

In lại 作者:行者123 更新时间:2023-12-03 03:41:29 27 4
mua khóa gpt4 Nike

如何循环遍历数组中的多个属性以及如何使用map函数将数组中的多个属性显示到网页

import React, { Component } from 'react';

import './App.css';

class App extends Component {
constructor(){
siêu();
this.state={
booksLists:'',
description: ''
}
}
componentWillMount() {
fetch('https://reactnd-books-api.udacity.com/books', { headers: {
'Authorization': 'whatever-you-want' }})
.then(res => res.json())
.then(res => {
this.setState({booksLists: res})
})
}

render() {
let booksLists = this.state.booksLists;
console.log(booksLists)

trở lại (

My Reads




);
}
}

导出默认应用程序;

1 Câu trả lời

这是一个 CodePen Demo ,或者您可以运行下面的代码片段:

class App extends React.Component {
constructor() {
siêu();
this.state = {
booksLists: []
};
}
componentWillMount() {
fetch("https://reactnd-books-api.udacity.com/books", {
headers: {
Authorization: "whatever-you-want"
}
}).then(res => res.json()).then(res => {
this.setState({booksLists: res.books});
});
}

render() {
const {booksLists} = this.state;
const books = booksLists
? booksLists.map(book =>


{book.title || 'not available'}


{book.subtitle || 'not available'}









Authors:
{book.authors
? book.authors.join(', ')
: 'not available'}



Publisher:
{book.publisher || 'not available'}



Date of publication:
{book.publishedDate || 'not available'}






{book.description || 'not available'}





)
: null;

trở lại (

My Reads


{books}

);
}
}

ReactDOM.render(
, document.getElementById("root"));





关于javascript - 进行 fetch 调用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45599832/

27 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com