Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

253
Vistas
React: How to access and change state/atributes via another component?

I want to change ComponentB atribute values from ComponentA (index currently).

My approach was to call lower component functions to change their state/values

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


E.g: Create a Task with name "Paul", data coming from another place like a TextField, DB...

When I try to do this it says changeName or changeState aren't functions. I assume because Tasks is returning a Div instead the actual class/object/data. I failed to access via props (assume it's read-only).

1.What JS/React concepts involve this problem (to study)

2.How to achieve the solution?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Thank you Andy for the help. I changed to return the whole class and ended up with this.

Higher Component

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

Lower Component

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda