0

I am following the project on github and working through personal modifications

It could probably be answered with google/documentation, but I don't know the right keywords to pull it in yet. One day, soon.

This doesn't line up with the file naming conventions I have seen.

Heroes.js imports api.js. api.js talks to the express backend via a proxy setting at the /api/* route. Problem is that the api.js file never declares an "api" component.

How is it that api.js is exporting "heroService", yet when its being imported, its operating under "api"? Is it because the file is named api.js and it defaults to that as the component name?

Heroes.js

import api from '../api';
//Example function
  handleDelete(event, hero) {
    event.stopPropagation();

    api.destroy(hero).then(() => {
      let heroes = this.state.heroes;
      heroes = heroes.filter(h => h !== hero);
      this.setState({ heroes: heroes });

      if (this.selectedHero === hero) {
        this.setState({ selectedHero: null });
      }
    });
  }

api.js

const baseAPI = '/api';    
const heroService = {
//example function


destroy(hero) {
    return new Promise((resolve, reject) => {
      fetch(`${baseAPI}/hero/${hero.id}`, { method: 'DELETE' })
        .then(response => response.json())
        .then(json => resolve(json))
        .catch(err => {
          reject(err);
        });
    });
  }

export default heroService;
Aaqib
  • 9,942
  • 4
  • 21
  • 30
giveemheller
  • 106
  • 8

1 Answers1

0

api.js is exporting a default.

Since this is not a named export, the binding takes on the name that you specified upon import.

As you wrote import api from '../api';, you use api.destroy().

If you wrote import monkeys from '../api';, you would use monkeys.destroy() instead.

References:

Jeevan Takhar
  • 491
  • 5
  • 10