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

325
Visualizações
i want to get the value of a text input, and create an <li> dynamically from my updated state, and then re-render the page

my app class component with it's state that get's updated when i press enter but doesn't immediately re-render the DOM


  constructor(props){
    super(props)
    
    this.state = {
      todoItemCurrent: '',
      todoList: []
    }

    this.handleChange = this.handleChange.bind(this)
    this.onSubmit = this.onSubmit.bind(this)
  }

  handleChange(e){
    this.setState({ todoItemCurrent: e.target.value }, ()=> {
      this.onSubmit(e)
    })    
  }

onSubmit(e){
  if(e.key === 'Enter'){
    this.state.todoList.push( { id: this.state.todoList.length + 1, todoItem: this.state.todoItemCurrent }  )
  }
  console.log( this.state)
  // console.log( this.state.todoList)
}

  render(){
    console.log(this.state.todoList)
    return (
      <div className="container">
        <header>
            <h1> TO-DO LIST APP</h1>
        </header>

   <TextBoxComponent value = { this.state.todoItemCurrent } placeholder = {`type here and hit enter to add an item`} handleChange = { this.handleChange } onEnter = {this.onSubmit} />

<ListComponentContainer todoList = { this.state.todoList } />
    </div>
    ); 
  }
}

export default App;

the text input component where i get the value from

const TextBoxComponent = ({ placeholder, handleChange, onEnter ,value })=> {
    return (
        <input className="text-box" type="text" value={ value } placeholder= { placeholder} onChange = { handleChange } onKeyDown = { onEnter } />
    );
}
export default TextBoxComponent

my list component, where i want to put the value in an li element and then append the li to the ul element

const ListComponentContainer = ( props ) => (   
    <ul> 
        { props.todoList.map( ({id, todoItem}) => (
            <li key={ id }> {todoItem} </li>
        )) }
    </ul>
);
export default ListComponentContainer
about 4 years ago · Juan Pablo Isaza
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