-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path8312e9e1.6cc03fa8.js
1 lines (1 loc) · 4.77 KB
/
8312e9e1.6cc03fa8.js
1
(window.webpackJsonp=window.webpackJsonp||[]).push([[28],{134:function(e,t,n){"use strict";n.d(t,"a",(function(){return u})),n.d(t,"b",(function(){return f}));var r=n(0),o=n.n(r);function a(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function c(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function i(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?c(Object(n),!0).forEach((function(t){a(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):c(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function s(e,t){if(null==e)return{};var n,r,o=function(e,t){if(null==e)return{};var n,r,o={},a=Object.keys(e);for(r=0;r<a.length;r++)n=a[r],t.indexOf(n)>=0||(o[n]=e[n]);return o}(e,t);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(r=0;r<a.length;r++)n=a[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(o[n]=e[n])}return o}var l=o.a.createContext({}),p=function(e){var t=o.a.useContext(l),n=t;return e&&(n="function"==typeof e?e(t):i(i({},t),e)),n},u=function(e){var t=p(e.components);return o.a.createElement(l.Provider,{value:t},e.children)},m={inlineCode:"code",wrapper:function(e){var t=e.children;return o.a.createElement(o.a.Fragment,{},t)}},d=o.a.forwardRef((function(e,t){var n=e.components,r=e.mdxType,a=e.originalType,c=e.parentName,l=s(e,["components","mdxType","originalType","parentName"]),u=p(n),d=r,f=u["".concat(c,".").concat(d)]||u[d]||m[d]||a;return n?o.a.createElement(f,i(i({ref:t},l),{},{components:n})):o.a.createElement(f,i({ref:t},l))}));function f(e,t){var n=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var a=n.length,c=new Array(a);c[0]=d;var i={};for(var s in t)hasOwnProperty.call(t,s)&&(i[s]=t[s]);i.originalType=e,i.mdxType="string"==typeof e?e:r,c[1]=i;for(var l=2;l<a;l++)c[l]=n[l];return o.a.createElement.apply(null,c)}return o.a.createElement.apply(null,n)}d.displayName="MDXCreateElement"},96:function(e,t,n){"use strict";n.r(t),n.d(t,"frontMatter",(function(){return c})),n.d(t,"metadata",(function(){return i})),n.d(t,"rightToc",(function(){return s})),n.d(t,"default",(function(){return p}));var r=n(3),o=n(7),a=(n(0),n(134)),c={id:"make-the-api-call-in-componentdidmount",sidebar_label:"Make the API call",title:"Make the API Call in componentDidMount()",description:"Make the API call in componentDidMount(), techniques, tips and tricks in development for Ract developer.",keywords:["make the api call in componentDidMount()","reactpatterns","react patterns","reactjspatterns","reactjs patterns","react","reactjs","react techniques","react tips and tricks"],version:"Make the API call in componentDidMount()",image:"/img/reactpatterns-cover.png"},i={unversionedId:"make-the-api-call-in-componentdidmount",id:"make-the-api-call-in-componentdidmount",isDocsHomePage:!1,title:"Make the API Call in componentDidMount()",description:"Make the API call in componentDidMount(), techniques, tips and tricks in development for Ract developer.",source:"@site/docs/make-the-api-call-in-componentdidmount.md",slug:"/make-the-api-call-in-componentdidmount",permalink:"/docs/make-the-api-call-in-componentdidmount",version:"current",sidebar_label:"Make the API call",sidebar:"someSidebar",previous:{title:"Proxy Component",permalink:"/docs/proxy-component"},next:{title:"Stateless Function (Presentational Component)",permalink:"/docs/stateless-function"}},s=[],l={rightToc:s};function p(e){var t=e.components,n=Object(o.a)(e,["components"]);return Object(a.b)("wrapper",Object(r.a)({},l,n,{components:t,mdxType:"MDXLayout"}),Object(a.b)("p",null,"You should populate data with AJAX calls in the ",Object(a.b)("inlineCode",{parentName:"p"},"componentDidMount")," lifecycle method. So you can use setState to update your component when the data is retrieved."),Object(a.b)("p",null,"For example using Class:"),Object(a.b)("pre",null,Object(a.b)("code",Object(r.a)({parentName:"pre"},{className:"language-jsx"}),"function componentDidMount() {\n fetch('api/sms')\n .then(result => {\n const sms = result.data\n console.log('COMPONENT WILL Mount messages : ', sms)\n this.setState({sms: [...sms.content]})\n })\n}\n")),Object(a.b)("p",null,"For example using Hook:"),Object(a.b)("pre",null,Object(a.b)("code",Object(r.a)({parentName:"pre"},{className:"language-jsx"}),"useEffect(() => {\n fetch('api/sms')\n .then(result => {\n const sms = result.data\n console.log('COMPONENT WILL Mount messages : ', sms)\n this.setState({sms: [...sms.content]})\n })\n}, [])\n")))}p.isMDXComponent=!0}}]);