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

184
Vistas
Rendering a box when submitting the data

I have the following code in my React:

const [property, setProperty] = useState([]);
  const [state, setState] = React.useState({ type: "", propertyName: "" });

const handleChange = (e, inputField) => {
    setState((prevState) => ({
      ...prevState,
      [inputField]: e.target.value,
    }));
  };

  const handleSubmit = () => {
    if (state.type !== "" && state.propertyName !== "") {
      const newObject = { type: state.type, propertyName: state.propertyName };
      property.push(newObject);
      console.log(property);
      setState({
        type: "",
        propertyName: "",
      });
    }
  };

And html:

          <div>
            <label htmlFor='properties' className='properties-label'>
              Properties
            </label>
            <div className='property-box'>
              <input
                type='text'
                id='type'
                value={state.type}
                placeholder='Type'
                className='type-element'
                required
                onChange={(e) => handleChange(e, "type")}
              ></input>
              <input
                type='text'
                id='name'
                value={state.propertyName}
                className='type-element'
                placeholder='Name'
                required
                onChange={(e) => handleChange(e, "propertyName")}
              ></input>
              <button
                className='buttonAccount'
                type='submit'
                onClick={handleSubmit}
              >
                Add Property
              </button>
            </div>
          </div>

What I want is when I press the add Property button a new html tag will render on the page(a box or something like that containing the two fields that has been inputted). Can you help me find a way to do that?

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

0

You can use the Javascript array map method to map each item in your property state into an HTML element.

For example:

  1. Make a function that returns the mapped property state into HTML elements.
const renderProperties = () => {
      return property.map((item, index) => (
           // `item` is a representation of each of your object in the property array
           // In this case, item contains { type: string, propertyName: string }
           <div key={index}> // React requires user to put a key in each of the mapped component
               <p>{item.propertyName}</p>
               <p>{item.type}</p>
           </div>
      ))
 }
  1. Call this function inside the HTML part of your JSX.
              ...
              <button
                className='buttonAccount'
                type='submit'
                onClick={handleSubmit}
              >
                Add Property
              </button>
            </div>
            {renderProperties()} // <-- Here
          </div>

https://reactjs.org/docs/lists-and-keys.html

about 4 years ago · Juan Pablo Isaza Denunciar

0

You have to print the elements in your property array. For exmaple:

{
  property.map((element) => (
    <div key={element.propertyName}>
      <span>
        {element.type}
      </span>
      <span>
        {element.propertyName}
      </span>
    </div>
  )
}
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