Search code examples
reactjsinfinite-loop

Reusable Component in React Infinite Loop


I am trying to make a dummy project that "adds an employee" with react. I have used a form with bulma css and an input component as a reusable component for this form. when i run npm start, its compiled successfully but the page never loads and i seem to have an infinite loop somewhere in my code. can anybody look at the code and let me know what I'm doing wrong?

import React, { Component } from 'react'
import Input from './input'

class EmployeeGeneral extends Component {
 state = {
   Employee: { FirstName: '', LastName: '', MeliCode: '' }
 }

 handleSubmit = e => {
   e.preventDefault()
 }

 handleChange = ({ currentTarget: input }) => {
   const Employee = { ...this.state.Employee }
   Employee[input.name] = input.value
   this.setState({ Employee })
 }
 render() {
   const { Employee } = this.state

   return (
     <main className="container">
       <h1 className="title">Employee General</h1>
       <form onSubmit={this.handleSubmit}>
         <div className="columns">
           <div className="column">
             <Input
               name="firstName"
               label="First Name"
               value={Employee.FirstName}
               onChange={this.handleChange}
             />

             <div className="control">
               <button className="button is-primary">Add Employee</button>
             </div>
           </div>
           <div className="column">
             <Input
               name="lastName"
               label="Last Name"
               value={Employee.LastName}
               onChange={this.handleChange}
             />
           </div>
           <div className="column">
             <Input
               name="meliCode "
               label="Meli Code"
               value={Employee.MeliCode}
               onChange={this.handleChange}
             />
           </div>
         </div>
       </form>
     </main>
   )
 }
}

export default EmployeeGeneral

import React from 'react'

const Input = ({ name, label, onChange, value }) => {
  return (
    <div className="field">
      <label htmlFor={name} className="label">
        {label}
      </label>
      <div className="control">
        <Input
          value={value}
          onChange={onChange}
          autoFocus
          id={name}
          type="text"
          className="input"
          placeholder={label}
          name={name}
        />
      </div>
      <p className="help">{label}</p>
    </div>
  )
}

export default Input


Solution

  • You are recursively using the Input component. Change the inner Input to a regular input instead.

    const Input = ({ name, label, onChange, value }) => {
      return (
        <div className="field">
          <label htmlFor={name} className="label">
            {label}
          </label>
          <div className="control">
            <input
              value={value}
              onChange={onChange}
              autoFocus
              id={name}
              type="text"
              className="input"
              placeholder={label}
              name={name}
            />
          </div>
          <p className="help">{label}</p>
        </div>
      )
    }