Me gustaría acceder a this.props.apiendpoint en api.js que se importó en test.js que es la clase base del componente.
# 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, } como fetchData() , tengo muchas funciones relacionadas con la API que pasan apiendpoint para cada función que no es una buena práctica.
cualquier ayuda será apreciada.
Agregaría apiEndPoint como argumento para la función fetchData.
# api.js import axios from 'axios'; export function fetchData(apiendpoint){ return axios.post(apiendpoint); }Y luego llámelo desde test.js pasando prop como parámetro.
import PropTypes from 'prop-types'; import {fetchData} from api.js; const test = ({ }) => { console.log(fetchData(this.props.apiendpoint)); }