Coder Social home page Coder Social logo

Comments (6)

egeriis avatar egeriis commented on May 20, 2024 1

@gazpachu In your case, dispatching those actions in componentWillMount of App should be sufficient.

from redux-json-api.

gazpachu avatar gazpachu commented on May 20, 2024

roman from Red Badger Team in the React London Slack group, was nice to give me this example. I'm just pasting it for your reference:

// define this inside your helper-module.js
import {
  createEntity,
  deleteEntity,
  readEndpoint,
  setAccessToken
  setEndpointHost,
  setEndpointPath
} from 'redux-json-api';
import { bindActionCreators } from 'redux';
import { API_URL, API_PATH } from '../../constants/constants';

const reduxJsonApiActions = {
  createEntity,
  deleteEntity,
  readEndpoint,
  setAccessToken
  setEndpointHost,
  setEndpointPath,
};

export default (dispatch, customActions) => ({
  ...bindActionCreators(reduxJsonApiActions, dispatch),
  configAPI: token => {
    dispatch(setEndpointHost(API_URL));
    dispatch(setEndpointPath(API_PATH));
    dispatch(setAccessToken(token));
  },
  readAPI: function (token, endpoint, callback) {
    this.configAPI(token, dispatch);
    dispatch(readEndpoint(endpoint)).then(function(data) {
      callback(data);
    });    
  },
  ...bindActionCreators(customActions, dispatch),
});

// inside your component.js
import getHelperActions from './helper-module.js';
import { bindActionCreators, connect } from 'redux';

const mapDispatchToProps = dispatch =>
  getHelperActions(dispatch, {
    yourAction,
    yourSecondAction
  });

class Cards extends Component {
  componentWillMount() {
    this.props.readAPI('token', 'http://endpoint', () => console.log('CALLBACK! ☎️'));
  }
  render() {
    ...
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(Cards);

from redux-json-api.

egeriis avatar egeriis commented on May 20, 2024

I am not sure in which context you would need to do this. Could you elaborate on the purpose of doing it this way and how we could support this in redux-json-api?

from redux-json-api.

gazpachu avatar gazpachu commented on May 20, 2024

Abstracting all the API related methods into a Helper module/component would free other components in the app from the repetition of having to call all the time setEndpointHost, setEndpointPath, setAccessToken, setHeader... That's a lot of config required for each API request, thus it's better to write it ONCE in one place rather than everytime we need to make an API request.

Maybe I'm missing something, but considering that in a React app, the order in which components mount is not really known or can be easily arranged, I needed to call all these previously mentioned methods prior to making the API request.

from redux-json-api.

egeriis avatar egeriis commented on May 20, 2024

You don't need to dispatch the configuring actions for each API request. The configuration is stored in state and will be used automatically for all requests.

from redux-json-api.

gazpachu avatar gazpachu commented on May 20, 2024

@egeriis And in which file do you usually set the configuration to make sure it runs before any other component?

If I put it in the componentDidMount of my app.jsx, that doesn't guarantee that the configuration will be set before any other component. Sometimes the App component is loaded after a few other components, we cannot change that. It's the way React loads components.

On the other hand, I cannot put it before my index.jsx file, before the react router configuration, because the dispatch method requires a React component, isn't it?:

ReactDOM.render(
      <Provider store={store}>
        <Router onUpdate={() => window.scrollTo(0, 0), logPageView} history={history}>
            <Route path="/" component={App}>
                <IndexRoute component={Home} />;
                <Route path="/favourites" component={Cards} />
                <Route path="/inbox" component={Cards} />
                <Route path="/insights" component={Insight} />
                <Route path="*" component={NotFound} />
            </Route>
        </Router>
      </Provider>
, document.getElementById('react-root'));

It would help a lot if you specify in the Docs where is recommended to set the configuration.

from redux-json-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.