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

251
Views
Reaccionar: ¿Cómo acceder y cambiar el estado/atributos a través de otro componente?

Quiero cambiar los valores del atributo ComponentB del ComponentA (índice actualmente).

Mi enfoque fue llamar a funciones de componentes inferiores para cambiar su estado/valores

 import React from 'react' class TaskComp extends React.Component{ constructor(props){ super(props); this.state = { name: "", starTime: 0, endTime: 0 } this.changeState = this.changeState.bind(this) // this.changeState = this.changeName.bind(this) } changeState(newName, newStart, newEnd){ // this.setState() this.state.name = newName this.state.starTime = newStart this.state.endTime = newEnd } } function changeName(newName){ this.state.name = newName } function componentDidMount() { // this.setState(this.state) } function renderTask(){ let task = new TaskComp() return <div> <p>{Object.keys(task.state).map((key) => <div key={key}>{key} {task.state[key]}</div>)}</p> </div> } export default renderTask

Por ejemplo: cree una tarea con el nombre "Paul", datos provenientes de otro lugar como un TextField, DB ...

Cuando trato de hacer esto, dice que changeName o changeState no son funciones. Supongo que porque Tasks está devolviendo un Div en lugar de la clase/objeto/datos reales. No pude acceder a través de accesorios (supongamos que es de solo lectura).

1.Qué conceptos de JS/React involucran este problema (a estudiar)

2.¿Cómo lograr la solución?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Gracias Andy por la ayuda. Cambié para devolver toda la clase y terminé con esto.

Componente Superior

 task.setState("Moe", 12,30) <p>{task.render()}</p>

Componente inferior

 import React from 'react' class Task extends React.Component { constructor(props) { super(props); this.state = { name: "", starTime: 0, endTime: 0 } // this.changeState = this.changeState.bind(this) // this.changeState = this.changeName.bind(this) } setState(newName, newStart, newEnd) { // this.setState() this.state.name = newName this.state.starTime = newStart this.state.endTime = newEnd } componentDidMount() { } render(){ return <div> {Object.keys(this.state).map((key) => <div key={key}>{key} {this.state[key]}</div>)} </div> } } export default Task
about 4 years ago · Juan Pablo Isaza 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!