Please fix those errors.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Please fix those errors.

import React from 'react';
class ContactForm extends React.Component {
 constructor(props) {
  super(props);
  this.state = {
   name: '',
   storageType: '',
   maxItem: '',
   resourceName: '',
   maxResource: '',
   resourceType: '',
   currentResource: '',
   display: false
  };
  this.handleNameChange = this.handleNameChange.bind(this);
  this.handleStorageTypeChange = this.handleStorageTypeChange.bind(this);
  this.handleMaxItemChange = this.handleMaxItemChange.bind(this);
  this.handleResourceNameChange = this.handleResourceNameChange.bind(this);
  this.handleResourceTypeChange = this.handleResourceTypeChange.bind(this);
  this.handleMaxResourceChange = this.handleMaxResourceChange.bind(this);
  this.handleCurrentResourceChange = this.handleCurrentResourceChange.bind(this);
  this.handleSubmit = this.handleSubmit.bind(this);
 }
 handleNameChange(event) {
  const target = event.target;
  const value = target.type === 'checkbox' ? target.checked : target.value;
  const name = target.name;

  this.setState({
   name: value
  })
  console.log('Change detected. State updated' + name + ' = ' + value);

 }
 handleStorageTypeChange(event) {
  const target = event.target;
  const value = target.type === 'checkbox' ? target.checked : target.value;

  this.setState({
   storageType: value
  })
 }
 handleMaxItemChange(event) {
  const target = event.target;
  const value = target.type === 'checkbox' ? target.checked : target.value;

  this.setState({
   maxItem: value
  })
 }
 handleResourceNameChange(event) {
  this.setState({
   resourceName: event.target.value
  })
 }
 handleResourceTypeChange(event) {
  this.setState({
   resourceType: event.target.value
  })
 }
 handleMaxResourceChange(event) {
  this.setState({
   maxResource: event.target.value
  })
 }
 handleCurrentResourceChange(event) {
  this.setState({
   currentResource: event.target.value
  })
 }
 handleSubmit() {
  this.setState({
   display: true
  })
 }
 displayButtons() {
  const back = <div>
   <input type="button" onClick={this.handleSubmit} value="Back" />
  <input type="button" onClick={this.handleSubmit} value="Edit" />
  <input type="button" onClick={this.handleSubmit} value="Delete" /></div>
  return back;
 }
 render() {
  return (
   <div>
    <form>
     <div className="form-group">
      <h1> Inventory </h1>
      <label for="nameInput">Name</label>
      <input type="text" name="name" value={this.state.name} onChange={this.handleNameChange}
       className="form-control" id="nameInput" placeholder="Name" />
     </div>
     <div className="form-group">
      <label for="storageType">Storage Type</label>
      <input name="storage" type="text" value={this.state.storageType} onChange={this.handleStorageTypeChange}
       className="form-control" id="storageType" placeholder="Storage Type" />
     </div>
     <div className="form-group">
      <label for="nameInput">Max Item Capacity</label>
      <input type="text" name="maxItem" value={this.state.maxItem} onChange={this.handleMaxItemChange}
       className="form-control" id="nameInput" placeholder="Max Item Capacity" />
     </div>
     <div className="form-group">
      <h1> Resources </h1>
      <label for="nameInput">Resource Name</label>
      <input type="text" name="resourceName" value={this.state.resourceName} onChange={this.handleResourceNameChange}
       className="form-control" id="nameInput" placeholder="Resource Name" />
     </div>
     <div className="form-group">
      <label for="nameInput">Resource Type</label>
      <input name="resourceType" value={this.state.resourceType} onChange={this.handleResourceTypeChange}
       className="form-control" id="nameInput" placeholder="Resource Type" />
     </div>
     <div className="form-group">
      <label for="nameInput">Max Number of Resources</label>
      <input name="maxResource" value={this.state.maxResource} onChange={this.handleMaxResourceChange}
       className="form-control" id="nameInput" placeholder="Max Number of Resources" />
     </div>
     <div className="form-group">
      <label for="nameInput">Current Number of Resources</label>
      <input type="text" name="currentResource" value={this.state.currentResource} onChange={this.handleCurrentResourceChange}
       className="form-control" id="nameInput" placeholder="Current number of Resources" />
     </div>
     <input type="button" onClick={this.handleSubmit} value="Submit form" />
     {this.state.display ? this.displayButtons() : ''}
    </form>
   </div>
  )
 }

}
class MainTitle extends React.Component {
 render() {
  return (
   <h1> Add Inventory </h1>
  )
 }
}
export default class App extends React.Component {
 render() {
  return (
   <div>
    <MainTitle />
    <ContactForm />
   </div>
  )
 }

}

The image displays a code snippet with several TypeScript errors, specifically relating to a React component for an inventory management system. The errors occur in a file named `AddInventory.tsx`. Below is a transcription and explanation of the issues shown in the image:

### Code and Error Breakdown

1. **Error TS2339**:
   - **Description**: `Property 'storageType' does not exist on type 'Readonly<{}>'`.
   - **Line Number**: 93
   - **Code**:
     ```jsx
     <label for="storageType">Storage Type</label>
     <input type="text" name="storageType" value={this.state.storageType} onChange={this.handleStorageTypeChange} />
     ```
   - **Explanation**: The code tries to access `this.state.storageType`, but `storageType` is not defined in the component's state.

2. **Error TS2322**:
   - **Description**: `Type '{ children: string; for: string; }' is not assignable to type 'DetailedHTMLProps<LabelHTMLAttributes<HTMLLabelElement>, HTMLLabelElement>'. Property 'for' does not exist on type 'DetailedHTMLProps<LabelHTMLAttributes<HTMLLabelElement>, HTMLLabelElement>'. Did you mean 'htmlFor'?`
   - **Line Number**: 97
   - **Code**:
     ```jsx
     <label for="nameInput">Max Item Capacity</label>
     ```
   - **Explanation**: The attribute `for` should be changed to `htmlFor` to be valid in JSX.

3. **Error TS2339**:
   - **Description**: `Property 'maxItem' does not exist on type 'Readonly<{}>'`.
   - **Line Number**: 99
   - **Code**:
     ```jsx
     <input type="text" name="maxItem" value={this.state.maxItem} onChange={this.handleMaxItemChange} />
     ```
   - **Explanation**: The code attempts to access `this.state.maxItem`, but `maxItem` is not defined in the component's state.

4. **Error TS2322**:
   - **Description**: Same as the previous TS2322 error regarding the incorrect use of `for` instead of `htmlFor`.
   - **Line Number**: 105
   - **Code**:
     ```jsx
Transcribed Image Text:The image displays a code snippet with several TypeScript errors, specifically relating to a React component for an inventory management system. The errors occur in a file named `AddInventory.tsx`. Below is a transcription and explanation of the issues shown in the image: ### Code and Error Breakdown 1. **Error TS2339**: - **Description**: `Property 'storageType' does not exist on type 'Readonly<{}>'`. - **Line Number**: 93 - **Code**: ```jsx <label for="storageType">Storage Type</label> <input type="text" name="storageType" value={this.state.storageType} onChange={this.handleStorageTypeChange} /> ``` - **Explanation**: The code tries to access `this.state.storageType`, but `storageType` is not defined in the component's state. 2. **Error TS2322**: - **Description**: `Type '{ children: string; for: string; }' is not assignable to type 'DetailedHTMLProps<LabelHTMLAttributes<HTMLLabelElement>, HTMLLabelElement>'. Property 'for' does not exist on type 'DetailedHTMLProps<LabelHTMLAttributes<HTMLLabelElement>, HTMLLabelElement>'. Did you mean 'htmlFor'?` - **Line Number**: 97 - **Code**: ```jsx <label for="nameInput">Max Item Capacity</label> ``` - **Explanation**: The attribute `for` should be changed to `htmlFor` to be valid in JSX. 3. **Error TS2339**: - **Description**: `Property 'maxItem' does not exist on type 'Readonly<{}>'`. - **Line Number**: 99 - **Code**: ```jsx <input type="text" name="maxItem" value={this.state.maxItem} onChange={this.handleMaxItemChange} /> ``` - **Explanation**: The code attempts to access `this.state.maxItem`, but `maxItem` is not defined in the component's state. 4. **Error TS2322**: - **Description**: Same as the previous TS2322 error regarding the incorrect use of `for` instead of `htmlFor`. - **Line Number**: 105 - **Code**: ```jsx
```plaintext
ERROR: Parameter 'props' implicitly has an 'any' type.
1 | import React from 'react';
2 | class ContactForm extends React.Component {
3 |   constructor(props) {
4 |     super(props);
5 |     this.state = {
6 |       name: ''
7 |     };
8 |   }
9 | }

ERROR in src/Views/AddInventory.tsx:24:19
TS7006: Parameter 'event' implicitly has an 'any' type.
22 |   this.handleSubmit = this.handleSubmit.bind(this);
23 | }
24 | handleNameChange(event) {
25 |   const target = event.target;
26 |   const value = target.type === 'checkbox' ? target.checked : target.value;
27 |   const name = target.name;
28 | }

ERROR in src/Views/AddInventory.tsx:35:26
TS7006: Parameter 'event' implicitly has an 'any' type.
33 | }
34 | 
35 | handleStorageTypeChange(event) {
36 |   const target = event.target;
37 |   const value = target.type === 'checkbox' ? target.checked : target.value;
38 | }

ERROR in src/Views/AddInventory.tsx:43:22
TS7006: Parameter 'event' implicitly has an 'any' type.
41 | }
42 | 
43 | handleMaxItemChange(event) {
44 |   const target = event.target;
45 |   const value = target.type === 'checkbox' ? target.checked : target.value;
46 | }
```

### Explanation:

The code displayed is from a TypeScript React application. In several places, there are compiler errors due to parameters implicitly having an 'any' type, which is not allowed with stricter TypeScript settings.

#### Common Issues Highlighted:
- **Type Inference Error**: The parameter 'event' in functions like `handleNameChange`, `handleStorageTypeChange`, and `handleMaxItemChange` lacks a specific type definition, causing TypeScript to throw an error.
  
#### Functions in the Code:
1. **handleNameChange**: Aims to update a component's state based on user input. Handles text input or checkboxes, setting state accordingly.
2. **handleStorageTypeChange & handleMaxItemChange**: Similar in structure to `handleNameChange`, these functions likely aim to handle different forms or types of user input.

#### Solution:
- Define explicit types
Transcribed Image Text:```plaintext ERROR: Parameter 'props' implicitly has an 'any' type. 1 | import React from 'react'; 2 | class ContactForm extends React.Component { 3 | constructor(props) { 4 | super(props); 5 | this.state = { 6 | name: '' 7 | }; 8 | } 9 | } ERROR in src/Views/AddInventory.tsx:24:19 TS7006: Parameter 'event' implicitly has an 'any' type. 22 | this.handleSubmit = this.handleSubmit.bind(this); 23 | } 24 | handleNameChange(event) { 25 | const target = event.target; 26 | const value = target.type === 'checkbox' ? target.checked : target.value; 27 | const name = target.name; 28 | } ERROR in src/Views/AddInventory.tsx:35:26 TS7006: Parameter 'event' implicitly has an 'any' type. 33 | } 34 | 35 | handleStorageTypeChange(event) { 36 | const target = event.target; 37 | const value = target.type === 'checkbox' ? target.checked : target.value; 38 | } ERROR in src/Views/AddInventory.tsx:43:22 TS7006: Parameter 'event' implicitly has an 'any' type. 41 | } 42 | 43 | handleMaxItemChange(event) { 44 | const target = event.target; 45 | const value = target.type === 'checkbox' ? target.checked : target.value; 46 | } ``` ### Explanation: The code displayed is from a TypeScript React application. In several places, there are compiler errors due to parameters implicitly having an 'any' type, which is not allowed with stricter TypeScript settings. #### Common Issues Highlighted: - **Type Inference Error**: The parameter 'event' in functions like `handleNameChange`, `handleStorageTypeChange`, and `handleMaxItemChange` lacks a specific type definition, causing TypeScript to throw an error. #### Functions in the Code: 1. **handleNameChange**: Aims to update a component's state based on user input. Handles text input or checkboxes, setting state accordingly. 2. **handleStorageTypeChange & handleMaxItemChange**: Similar in structure to `handleNameChange`, these functions likely aim to handle different forms or types of user input. #### Solution: - Define explicit types
Expert Solution
steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY