gpt4 book ai didi

javascript - 在 reactjs 组件上渲染多个项目

转载 作者:数据小太阳 更新时间:2023-10-29 04:15:20 25 4
gpt4 key购买 nike

我正在实现抽象工厂并且 getitems 方法工作并返回我模拟的 2 个项目,但是我不确定如何在 react 组件中呈现这些项目

我的代码在下面

抽象工厂.tsx

import * as React from 'react';
import { IAbstractFactoryProps } from "./IAbstractFactoryProps";
import { IAbstractFactoryState } from "./IAbstractFactoryState";
import styles from './Abstractfactory.module.scss';
import { escape } from '@microsoft/sp-lodash-subset';
import DaoFactory from "./DaoFactory";
import ICustomerDao from "./ICustomerDao";
import DataSources from "./DatasourcesEnum";

export default class Abstractfactory extends React.Component<IAbstractFactoryProps, {}> {
private customerDao: ICustomerDao;

constructor(props: IAbstractFactoryProps, state: IAbstractFactoryState) {
super(props);
this.setInitialState();
this.setDaos(props.datasource);

this.state = {
items: this.customerDao.listCustomers(),
};
}

public render(): React.ReactElement<IAbstractFactoryProps> {
return (
<div className={ styles.abstractfactory }>
<div className={ styles.container }>
<div className={ styles.row }>
<div className={ styles.column }>
{this.state.items}
</div>
</div>
</div>
</div>
)
}

public setInitialState(): void {
this.state = {
items: []
};
}

private setDaos(datasource: string): void {
const data: any = datasource === "Sharepoint" ? DataSources.SharepointList : DataSources.JsonData;
this.customerDao = DaoFactory.getDAOFactory(data).getCustomerDAO();

//Now, its transparent for us a UI developers what datasource was selected
//this.customerDao.
}
}

客户.ts

class Customer{  
public id: string;
public firstName: string;
public lastName: string;
}

export default Customer;

数据源编号.ts

enum DataSources {  
SharepointList = "SharepointList",
JsonData = "JsonData"
}

export default DataSources;

道工厂

import ICustomerDAO from "./ICustomerDAO";  

import DataSources from "./DatasourcesEnum";

abstract class DAOFactory {

public abstract getCustomerDAO(): ICustomerDAO;

public static getDAOFactory(whichFactory: DataSources): DAOFactory {
switch (whichFactory) {
case DataSources.SharepointList:
return new SharepointListDAOFactory();
case DataSources.JsonData:
return new JsonDAOFactory();
default :
return null;
}
}
}

export default DAOFactory;
import SharepointListDAOFactory from "./SharepointListDAOFactory";
import JsonDAOFactory from "./JsonDAOFactory";

JsonDaoFactory.ts

import DAOFactory from "./DaoFactory";  
import JsonCustomerDAO from "./JsonCustomerDAO";
import ICustomerDao from "./ICustomerDao";

class JsonDAOFactory extends DAOFactory {
public getCustomerDAO(): ICustomerDao{
return new JsonCustomerDAO();
}
}

export default JsonDAOFactory;

SharepointListDaoFactory.ts

import DaoFactory from "./DaoFactory";  
import ICustomerDao from "./ICustomerDao";
import SharepointCustomerDao from "./SharepointCustomerDAO";

class SharepointListDAOFactory extends DaoFactory {
public getCustomerDAO(): ICustomerDao{
return new SharepointCustomerDao();
}
}

export default SharepointListDAOFactory;

JsonCustomerDao.ts

import ICustomerDao from "./ICustomerDao";  
import Customer from "./Customer";

class JsonCustomerDAO implements ICustomerDao{
public insertCustomer(): number {
// implementation to be done by reader
return 1;
}

public deleteCustomer(): boolean {
// implementation to be done by reader
return true;
}

public findCustomer(): Customer {
// implementation to be done by reader
return new Customer();
}

public updateCustomer(): boolean {
// implementation to be done by reader
return true;
}

public listCustomers(): Customer[] {
// implementation to be done by reader
let c1: Customer= new Customer();
let c2: Customer= new Customer();
c1.id="3";
c1.firstName="Andrew";
c1.lastName="Valencia";
c2.id="4";
c2.firstName="Charles";
c2.lastName="Smith";


let list: Array<Customer> = [c1, c2 ];
return list;
}
}

export default JsonCustomerDAO;

SharepointCustomerDao.ts

import ICustomerDao from "./ICustomerDao";  
import Customer from "./Customer";

class SharepointCustomerDao implements ICustomerDao {
public insertCustomer(): number {
// implementation to be done by reader
return 1;
}

public deleteCustomer(): boolean {
// implementation to be done by reader
return true;
}

public findCustomer(): Customer {
// implementation to be done by reader
return new Customer();
}

public updateCustomer(): boolean {
// implementation to be done by reader
return true;
}

public listCustomers(): Customer[] {
// implementation to be done by reader
let c1: Customer = new Customer();
c1.id="1";
c1.firstName="Luis";
c1.lastName="Valencia";
let c2: Customer = new Customer();
c2.id="2";
c2.firstName="John";
c2.lastName="Smith";
let list: Array<Customer> = [c1, c2 ];
return list;
}
}

export default SharepointCustomerDao;

客户道

import Customer from "./Customer";

interface ICustomerDao {
insertCustomer(): number;
deleteCustomer(): boolean;
findCustomer(): Customer;
updateCustomer(): boolean;
listCustomers(): Customer[];
}

export default ICustomerDao;

.tsx

import * as React from 'react';
import { IAbstractfactoryProps } from "./IAbstractFactoryProps";
import { IAbstractFactoryState } from "./IAbstractFactoryState";
import styles from './Abstractfactory.module.scss';
import { escape } from '@microsoft/sp-lodash-subset';
import DaoFactory from "./DaoFactory";
import ICustomerDao from "./ICustomerDao";
import DataSources from "./DatasourcesEnum";

export default class Abstractfactory extends React.Component<IAbstractfactoryProps, {}> {
private customerDao: ICustomerDao;

constructor(props: IAbstractfactoryProps, state: IAbstractFactoryState) {
super(props);
this.setInitialState();
this.setDaos(props.datasource);
}

public render(): React.ReactElement<IAbstractfactoryProps> {
this.state = {
items: this.customerDao.listCustomers(),
};

return null;
}

public setInitialState(): void {
this.state = {
items: []
};
}

private setDaos(datasource: string): void {
const data: any = datasource === "Sharepoint" ? DataSources.SharepointList : DataSources.JsonData;
this.customerDao = DaoFactory.getDAOFactory(data).getCustomerDAO();
}
}

更新 1

我改变了渲染方法如下

 public render(): React.ReactElement<IAbstractFactoryProps> {
return (
<div className={ styles.abstractfactory }>
<div className={ styles.container }>
<div className={ styles.row }>
<div className={ styles.column }>
{this.state.items.map( i => (<div>i.id</div>)}
</div>
</div>
</div>
</div>
);
}

我有这两个问题

[23:11:06] Error - typescript - src/webparts/abstractfactory/components/Abstractfactory.tsx(34,63): error TS1005: ',' expected.
[23:11:06] Error - typescript - src/webparts/abstractfactory/components/Abstractfactory.tsx(34,30): error TS2339: Property 'items' does not exist on type 'Readonly<{}>'.

最佳答案

** 更新 **好的,那么让 listCustomers 方法保持原样。

根据你的代码和render方法的内容items 等于客户数组对吗?如果是,则替换

{this.state.items}

{this.state.items.map( i => (<div>i.id</div>)}

例如。

旁注:React 在状态改变时重新渲染组件。因此,每当您对 customerList 进行更新时,请确保将其置于呈现它们的组件的状态。

关于javascript - 在 reactjs 组件上渲染多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50545924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com