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

279
Visualizações
Adding input from textarea/form to a list with React

I'm studying to become a Front-End Developer and now react is on the menu. I need to create a react-app that has a grocery list and a shopping cart. I can add items to the grocery list and eventually I can click on them to transfer them to the shopping cart.

The whole react tree-like experience is quite new and overwhelming. But my question is, I need to add the data that I get from an input field to a UL/LI.

I have a GroceryList component, an InputField component, a List component, and a ListItem component.

The code I have now for the InputField works kind of but it displays the information directly on the screen and not on submit.

The code is:

import React, { useState } from "react";
import { ReactDOM } from "react";


function InputField(props) {



    return (

        < div >
            <form>
                <input type="text" onChange={(event) => props.setGroceryName(event.target.value)} />
                <button type="submit" >Add Groceries</button>
            </form>
        </div >
    )
    
}


export default InputField

I defined the setGroceryName in the GroceryList component. Can someone help me with how I can change it so it makes a list in the List component? I'm really stuck and can't seem to find some good help. Or what do I need to do and where do I need to put it so it takes a ListItem and adds that to the List component.

Thanks in advance. If you guys need more info, let me know.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

import React, { Component } from 'react'

export default class Test extends Component {

    state = {
        grocery: '',
        groceryList: []
    }
    render() {
        return (
            <div>
                <form>
                    <input type="text" onChange={(e) => this.setState({ grocery: e.target.value })} />
                    <button type="submit" onClick={e => this.setState({ groceryList: [...this.state.groceryList, this.state.grocery], grocery: '' })}>Add Groceries</button>
                </form>
            </div>
        )
    }
}

You need to setState for every time the text is changing for a new grocery item and when clicked on the button, add it to the groceryList array, and set the initial text to an empty array.

about 4 years ago · Santiago Trujillo 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