Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

259
Vistas
Implement recursive onClick event in react js

I am trying to implement a recursive method on reactjs, but only when data is submitted or clicked.

Following is the code I am trying with:

import React, { Component } from "react";

class Textbox extends React.Component {
    constructor(props) {
      super(props);
      this.state = {value: ''};
      this.handleChange = this.handleChange.bind(this);
      this.handleSubmit = this.handleSubmit.bind(this);
    }
  
    handleChange(event) {this.setState({value: event.target.value});  }
    handleSubmit(event) {
      alert('A name was submitted: ' + this.state.value);
      event.preventDefault();
    }
  
    render() {
      return (
        <form onSubmit={this.handleSubmit}>
        <div> 
          <label>
            Name:
            <input type="text" value={this.state.value} onChange={this.handleChange} />
          </label>
          <input type="submit" value="React" />
        </div> 
        </form>
      );
    }
  }

export default Textbox;

which generates the following view.

A simple text box view

I want to use a recursive method onClick or onSubmit, such that it will generate another text box upon submission. Like

View of the new text box after the first submission.

which I want to continue until I click some "Exit" or "Stop" button, which I can add to the top of the view.

From what I have read about recursive implementation on ReactJS, I need to call the class/function again inside render. When I do that I think react is getting inside the infinite loop and freezes the browser.

what I tried is to call Textbox inside the <div> <div/> of the render method. Like this:

.... other code lines are same
<div> 
  <label>
    Name:
    <input type="text" value={this.state.value} onChange={this.handleChange} />
  </label>
  <input type="submit" value="React" />
  <Textbox/>
</div> 

How can I generate a recursive textbox on submission/clicking event on the previous text box?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could do it like this, where showNextInput prevents the infinite loop:

class Textbox extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            value: '',
            showNextInput: false,
        };
        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
    }

    handleChange(event) {this.setState({value: event.target.value});  }
    handleSubmit(event) {
        console.log('submit');
        event.preventDefault();
        this.setState({ showNextInput: true });
    }

    render() {
        return (
            <>
                <form onSubmit={this.handleSubmit}>
                    <div>
                        <label>
                            Name:
                            <input type="text" value={this.state.value} onChange={this.handleChange} />
                        </label>
                        <input type="submit" value="Submit" />
                    </div>
                </form>
                { this.state.showNextInput ? <Textbox/> : null }
            </>
        );
    }
}

However, your use case looks like something you would usually do by

  • managing a list of values somewhere,
  • add items as required inside your handlers, and
  • then display a list of these items

Here a quick and dirty example:

export class TextboxList extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            values: {
                0: ''
            },
        };
        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
    }

    handleChange(index, value) {
        this.setState({
            values: {
                ...this.state.values,
                [index]: value
            }
        });
    }

    handleSubmit(event) {
        event.preventDefault();
        this.setState({
            values: {
                ...this.state.values,
                [Object.keys(this.state.values).length]: '' // add new empty item to list
            }
        });

    }

    render() {
        return (
            <>
                <form onSubmit={this.handleSubmit}>
                    { Object.keys(this.state.values).map( (index) => {
                        const value = this.state.values[index];
                        return <div key={ index }>
                            <label>
                                Name:
                                <input
                                    type="text"
                                    value={ value }
                                    onChange={ (event) => this.handleChange( index, event.target.value ) }
                                />
                            </label>
                            <input type="submit" value="Submit" />
                        </div>;
                    })}
                </form>
            </>
        );
    }
}

export default Textbox;
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda