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

125
Vistas
How to use props value in non component class which imported in component in React

I would like to access this.props.apiendpoint in api.js which imported in test.js which is component base class.

# api.js
import axios from 'axios';
export function fetchData(){
   return axios.post(apiendpoint);
}
# App.js
import test from test.js
export default class App extends Component {
  constructor() {
    this.apiendpoint = 'api.example.com';
  }
   
  render() {
    <test apiendpoint = {apiendpoint} />
  }
}

# test.js
import PropTypes from 'prop-types';
import {fetchData}  from api.js;

const test = ({
}) => {
   console.log(this.props.apiendpoint); // this value how to acccess in api.js 
   fetchData();
}
test.propTypes = {
apiendpoint: PropTypes.String,
}
test..defaultProps = {
apiendpoint : null,
}

like fetchData() I have many functions for API related passing apiendpoint for each function not good practice.

any help will be appreciated.

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

0

I would add apiEndPoint as an argument to fetchData function.

# api.js
import axios from 'axios';
export function fetchData(apiendpoint){
   return axios.post(apiendpoint);
}

And then call it from test.js by passing prop as a parameter.

import PropTypes from 'prop-types';
import {fetchData}  from api.js;

const test = ({
}) => {
   console.log(fetchData(this.props.apiendpoint));

}
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