0

I need to generate an array of 10 items and this is the code I have to use.

for (let i = 0; i < 10; i++) {
  vehicles.push({
    manufacturer: faker.vehicle.manufacturer(),
    model: faker.vehicle.model(),
    type: faker.vehicle.type(),
    fuel: faker.vehicle.fuel(),
    vin: faker.vehicle.vin(),
    color: faker.vehicle.color()
  })
}

My question is how do I use it? So far I have this:

import React from 'react';
import Stack from 'react-bootstrap/Stack'
import VehicleActions from './VehicleActions';
import VehicleList from './VehicleList';
import Vehicle from './Vehicle';

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { vehicles: [] };
    this.onNewContact = this.onNewContact.bind(this);
    this.handleContactSelected = this.handleContactSelected.bind(this)
    this.handleDeleteContact = this.handleDeleteContact.bind(this)
    
  }

  onNewContact(vehicle) {
    this.setState((state) => {
      return {
        vehicles: state.vehicles.concat(vehicle)
      }
    })
  }

  handleContactSelected(manufacturer) {
    this.setState((state) => {
      return {
        selected: manufacturer
      }
    })
  }
  
  handleDeleteContact(manufacturer) {
    this.setState((state) => {
      return {
        vehicles: state.vehicles.filter((vehicle) => vehicle.manufacturer !== manufacturer),
        selected: null
      }
    })
  }

  handleGenerateVehicle() {
    this.props.onNewContact(this.generateContact())
  }

  render() {
    return (
      
      <Stack gap={3} className="col-md-10 mx-auto">
        <VehicleActions onNewContact={this.onNewContact}
                        selectedContact={this.state.selected} />
        <Vehicle />
        <VehicleList vehicles={this.state.vehicles}
        onContactSelected = {this.handleContactSelected}/>
      </Stack>
      
    )
  }
}

export default App

I'm using the "npm install react-bootstrap bootstrap @faker-js/faker" to generate the random text, I have tested it in the alert or console log and it works, but I have no clue where to insert it so the 10 values are shown in a column.

1 Answer 1

1

If you want to hardcode the list of vehicles (I assume that is what you are supposed to do), just put the code in the constructor:

class App extends React.Component {
  constructor(props) {
    super(props);
    let vehicles = [];
    for (let i = 0; i < 10; i++) {
      vehicles.push({
        manufacturer: faker.vehicle.manufacturer(),
        model: faker.vehicle.model(),
        type: faker.vehicle.type(),
        fuel: faker.vehicle.fuel(),
        vin: faker.vehicle.vin(),
        color: faker.vehicle.color()
      })
    }
    this.state = { vehicles };
    this.onNewContact = this.onNewContact.bind(this);
    this.handleContactSelected = this.handleContactSelected.bind(this)
    this.handleDeleteContact = this.handleDeleteContact.bind(this) 
  }

  // ...

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answering! I did it and now it says "faker is not defined" in every single line. I declared it in another file and imported it. generateVehicle() { let manufacturer = faker.vehicle.manufacturer() let model = faker.vehicle.model() let type = faker.vehicle.type() let fuel = faker.vehicle.fuel() let vin = faker.vehicle.vin() let color = faker.vehicle.color() return { manufacturerN: manufacturer, modelN: model, typeN: type, fuelN: fuel, vinN : vin, colorN: color }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.