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

178
Vistas
How to access props from Parent Component inside the Child Class Component ? (specifically for componentDidMount)?

I want to use the {prop.idno} from parent to child in componentDidMount() fetch call URL . How do I make use of idno in the URL to call different posts pages.

Is there a way to write it like this?

const response = await fetch(
       `https://jsonplaceholder.typicode.com/posts?userId= ${this.prop.idno}`
      );

Parent Component :

return (
<>
  <TogglePosts idno = {user.id}/>
</>
)

Child Component:

export class TogglePosts extends Component {
  constructor(props){
    super(props);
    this.state = {
      on: false,
      data: [],
      loaded: false,
    };
  }

  toggle = () => {
    this.setState({
      on: !this.state.on,
    });
  };

  async componentDidMount() {
    console.log("comp did mount");
    try {
      const response = await fetch(
        `https://jsonplaceholder.typicode.com/posts?userId=1`
      );
      const data = await response.json();
      console.log(data);

      this.setState({ data: data, loaded: true });
    } catch (err) {
      console.log(err);
    }
  }

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

0

You can try editing your response similar to this.

    const response = await fetch(
        `https://jsonplaceholder.typicode.com/posts?userId= ${this.props.idno}`
      );

Note that I've used `(tilde) instead of "(double quotes) Also use props instead of prop

about 4 years ago · Juan Pablo Isaza Denunciar

0

Can you try accessing it using props.idno in the TogglePosts Component? In the fetch statement you can do something like fetch(`https://jsonplaceholder.typicode.com/posts?userId=${props.idno}`);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try this:

const response = await fetch(
        `https://jsonplaceholder.typicode.com/posts?userId= ${this.props.idno}`
      );

Template literal(``) is the ability to include expressions and variables within a string.

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