Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

187
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda