簡體   English   中英

用於制作多個類組件的React函數

[英]React function for making multiple class components

所以,我有一個類組件從API加載一些數據:

class Item extends Component {
constructor(props) {
  super(props);
  this.state = {
    output: {}
  }
}

componentDidMount() {
    fetch(item_url[0])
      .then(response => response.json())
      .then(data => this.setState({ output: data }));
  }


render() {
    console.log(this.state.output);
    const { general = {name:"", description:""} } = this.state.output;
    const { brand = {name : ""} } = this.state.output;
    const { id } = this.state.output;
    const {images = {primary:{large:""}}} = this.state.output;
  return (
    <ItemPanel>
    <ItemBox>
    <BoxTitle>{general.name}</BoxTitle>
    <BoxId>Item ID: {id}</BoxId>
    <Details onClick={show_details}>Show more...</Details>
        <Inline>
        <Quantity type="number" defaultValue="1"></Quantity>
        <Icon>add_shopping_cart</Icon>
        </Inline>
        <AddItem>
        <Sfont>Add to cart</Sfont>
    </AddItem>
    </ItemBox>
        <BoxImg src={images.primary.large} alt='img error'></BoxImg>
   </ItemPanel>
  );
}
}
export default Item;

它可以正常使用API​​,從這個數組插入地址(URL):

let item_url = [
'http://localhost:3005/products/774944', 
'http://localhost:3005/products/774945', 
'http://localhost:3005/products/774946',
'http://localhost:3005/products/123581', 
'http://localhost:3005/products/782691', 
'http://localhost:3005/products/782485',
'http://localhost:3005/products/782486', 
'http://localhost:3005/products/782487', 
'http://localhost:3005/products/782488',
'http://localhost:3005/products/738471'];

我想在這里實現的是一個多次渲染此組件的函數(每次都使用另一個API數據)。 我想這里需要某種循環功能,但無法弄明白。 它現在從我的index.js渲染,如下所示:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import Item from './DataHarvester'

ReactDOM.render([<Item />, <App />], document.getElementById('root'));

但顯然,它只創建了一個組件,而我需要有10個組件。

你可以使用map來做到這一點; 通常模式如下:

  1. 父組件請求數據並將結果置於某種形式的狀態
  2. 在父組件的渲染內, map該狀態以生成子組件
  3. 然后,子組件可以呈現從父項傳遞的數據

 const Section = (props) => ( <p>{props.url}</p> ); const App = () => { const urls = [ 'http://localhost:3005/products/774944', 'http://localhost:3005/products/774945', 'http://localhost:3005/products/774946', 'http://localhost:3005/products/123581', 'http://localhost:3005/products/782691', 'http://localhost:3005/products/782485', 'http://localhost:3005/products/782486', 'http://localhost:3005/products/782487', 'http://localhost:3005/products/782488', 'http://localhost:3005/products/738471' ]; return ( <div> {urls.map(x => <Section url={x}/>)} </div> ); }; ReactDOM.render(<App />, document.getElementById('r')); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="r"></div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM