Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
pasar el valor del estado como accesorios a otro componente es6

Puedo pasar datos pero estoy atascado aquí, ¿cómo pasar el estado del AddTodo component Todos component ? ¿Hay algún problema con la estructura de mi componente?

 class AddTodo extends React.Component { constructor(){ super(); this.state = {text: ''}; } onTextChanged(e){ this.setState({text:e.target.value}); } addHandler(){ alert(this.state.text); // pass this to Todos?? } render() { return( <div> <input type="text" onChange={(e)=>this.onTextChanged(e)} /> <button onClick={()=>this.addHandler()}>Add</button> </div> ) } } class Todos extends React.Component { constructor(){ super(); this.data = ['write book','wash clothes','jogging']; } render() { return ( <div> <AddTodo/> <ul> {this.data.map((item)=><TodoItems key={item} item={item}/>)} </ul> </div> ); } }

Demostración en vivo aquí http://jsbin.com/susadehacu/1/edit

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Muy simple, solo necesita agregar un método en Todos para agregar un elemento a su lista. Esto luego se pasaría a su componente AddTodo como accesorio.

Todos

 constructor(){ super(); this.data = ['write book','wash clothes','jogging']; } addTodo(todo) { // new array let newData = this.data.slice(); newData.push(todo); this.setState({data: newData}); } ... <AddTodo addTodo={(todo) => this.addTodo(todo)} />

Añadir Todo

 addHandler(){ this.props.addTodo(this.state.text); }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!