JavaScript How to use setState in external function? setstate in external function,react setstate from external function

import Request from 'superagent';

const getApi = () => {
    let url = '/* URL */';
    return Request.get(url).then((res) => {
        this.setState({
             content: res.body
        });
    });
}

export default getApi;

I've created function in external file for api call. How do I get access to function/setState in external file?

componentWillMount(){
   getApi();
}

I receive this error:

'TypeError: Cannot read property 'setState' of undefined'

Answer:1

You can pass in a callback to the getApi function from your component.

const getApi = (onSuccess) => {
    let url = '/* URL */';
    return Request.get(url).then((res) => {
        onSuccess(res.body);
    });
}

Component

componentWillMount(){
   getApi(this.setApiResponse);
}

setApiResponse(data) {
    this.setState({
        content: data
    });
}

NB. As your project grows larger, you may wish to look into a state management system, such as Redux.

Answer:2

Technically you could pass the instance to getApi so that it knows what this is supposed to be. But imo. that's a poor approach. It creates unnecessary dependencies. At the moment, getApi() needs to know how the React class works; the function is responsible to manipulate the state of the Instance correctly.

Better if getApi() only returns the value, and the instance consumes it:

const getApi = () => {
    let url = '/* URL */';
    return Request.get(url).then(res => res.body);
}

componentWillMount(){
   getApi().then(content => this.setState({ content }));
}
Answer:3

With the help of the following code i am able to embed google search bar within my website <iframe id="if1" width="100%" height="254" style="visibility:visible" src="http://www.google.com/custom?q=...

With the help of the following code i am able to embed google search bar within my website <iframe id="if1" width="100%" height="254" style="visibility:visible" src="http://www.google.com/custom?q=...

I want to add the Shipping and Billing addresses form in the cart.liquid file so it is possible to add these two addresses form in cart page if anybody known then give solution for this.

I want to add the Shipping and Billing addresses form in the cart.liquid file so it is possible to add these two addresses form in cart page if anybody known then give solution for this.

  1. shipping billing addresses
  2. shipping billing address difference
  3. billing/shipping address mismatch
  4. billing shipping address javascript
  5. shipping address billing paypal
  6. billing shipping address form
  7. shipping address billing address
  8. shipping address billing information
  9. shipping vs billing address
  10. shipping and billing address don't match
  11. shipping and billing address in amazon
  12. shipping and billing address lazada
  13. shipping and billing address meaning
  14. shipping and billing address must match
  15. supreme shipping billing address
  16. amazon shipping billing address different
  17. drop shipping billing address
  18. shipping cart billing address
  19. ebay shipping billing address different
  20. shipping and billing address in gst

How do I access the paper-button with id='signin' outside of the Polymer constructor. I need to access it from Template.{template-name}.rendered method. Is there a way to use selector for paper-button?...

How do I access the paper-button with id='signin' outside of the Polymer constructor. I need to access it from Template.{template-name}.rendered method. Is there a way to use selector for paper-button?...

  1. access shadow dom polymer
  2. polymer access shadow dom element
  3. polymer access shadow root

I have an input <input type="text" ng-model="choice.text"> Where choice.text creates a new text property in the object choice. This allows me to send the contents of the input to different ...

I have an input <input type="text" ng-model="choice.text"> Where choice.text creates a new text property in the object choice. This allows me to send the contents of the input to different ...

  1. model values django
  2. model values in javascript
  3. model values in jquery
  4. model values in c#
  5. model value binding
  6. model values rails
  7. model value reset
  8. model value data
  9. model values mongo
  10. values model theory
  11. values model in python
  12. values model selection
  13. django model values_list
  14. model train values
  15. model car values
  16. park model values
  17. role model values
  18. model_mean_values
  19. model t values
  20. iceberg model values beliefs