-
Notifications
You must be signed in to change notification settings - Fork 3
/
d2aa43a7.98fddb0d.js
1 lines (1 loc) · 5.46 KB
/
d2aa43a7.98fddb0d.js
1
(window.webpackJsonp=window.webpackJsonp||[]).push([[50],{116:function(e,t,n){"use strict";n.r(t),n.d(t,"frontMatter",(function(){return o})),n.d(t,"metadata",(function(){return s})),n.d(t,"rightToc",(function(){return p})),n.d(t,"default",(function(){return l}));var r=n(3),a=n(7),i=(n(0),n(134)),o={id:"props-in-initial-state-is-an-anti-pattern",sidebar_label:"Props in initial state is an anti-pattern",title:"Props in Initial State is an Anti-pattern",description:"Props in initial state is an anti-pattern | React Patterns, techniques, tips and tricks in development for Ract developer.",keywords:["props in initial state is an anti-pattern","reactpatterns","react patterns","reactjspatterns","reactjs patterns","react","reactjs","react techniques","react tips and tricks"],version:"Props in initial state is an anti-pattern",image:"/img/reactpatterns-cover.png"},s={unversionedId:"props-in-initial-state-is-an-anti-pattern",id:"props-in-initial-state-is-an-anti-pattern",isDocsHomePage:!1,title:"Props in Initial State is an Anti-pattern",description:"Props in initial state is an anti-pattern | React Patterns, techniques, tips and tricks in development for Ract developer.",source:"@site/docs/props-in-initial-state-is-an-anti-pattern.md",slug:"/props-in-initial-state-is-an-anti-pattern",permalink:"/docs/props-in-initial-state-is-an-anti-pattern",version:"current",sidebar_label:"Props in initial state is an anti-pattern",sidebar:"someSidebar",previous:{title:"Pure Component Avoid Heavy Re-render",permalink:"/docs/pure-component-avoid-heavy-re-render"},next:{title:"shouldComponentUpdate Avoid Heavy Re-render",permalink:"/docs/shouldcomponentupdate-avoid-heavy-re-render"}},p=[],c={rightToc:p};function l(e){var t=e.components,n=Object(a.a)(e,["components"]);return Object(i.b)("wrapper",Object(r.a)({},c,n,{components:t,mdxType:"MDXLayout"}),Object(i.b)("p",null,'Using props to generate state in constructor ( or getInitialState) often leads to duplication of "source of truth", for example where the real data is. This is because constructor (or getInitialState) is only invoked when the component is first created.'),Object(i.b)("p",null,"The danger is that if the props on the component are changed without the component being 'refreshed', the new prop value will never be displayed because the constructor function (or getInitialState) will never update the current state of the component. The initialization of state from props only runs when the component is first created."),Object(i.b)("p",null,"The bad one."),Object(i.b)("pre",null,Object(i.b)("code",Object(r.a)({parentName:"pre"},{className:"language-jsx"}),"class UserPassword extends Component {\n // Constructor (or getInitialState)\n constructor(props) {\n super(props)\n this.state = {\n confirmed: false,\n inputVal: props.inputValue\n }\n }\n\n render() {\n return <div>{this.state.inputVal && <AnotherComponent/>}</div>\n }\n}\n")),Object(i.b)("p",null,"The good one."),Object(i.b)("pre",null,Object(i.b)("code",Object(r.a)({parentName:"pre"},{className:"language-jsx"}),"class UserPassword extends Component {\n // Constructor function (or getInitialState)\n constructor(props) {\n super(props);\n this.state = {\n confirmed: false\n }\n }\n\n render() {\n return <div>{this.props.inputValue && <AnotherComponent/>}</div>\n }\n}\n")))}l.isMDXComponent=!0},134:function(e,t,n){"use strict";n.d(t,"a",(function(){return u})),n.d(t,"b",(function(){return m}));var r=n(0),a=n.n(r);function i(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(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 s(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?o(Object(n),!0).forEach((function(t){i(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):o(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function p(e,t){if(null==e)return{};var n,r,a=function(e,t){if(null==e)return{};var n,r,a={},i=Object.keys(e);for(r=0;r<i.length;r++)n=i[r],t.indexOf(n)>=0||(a[n]=e[n]);return a}(e,t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(r=0;r<i.length;r++)n=i[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(a[n]=e[n])}return a}var c=a.a.createContext({}),l=function(e){var t=a.a.useContext(c),n=t;return e&&(n="function"==typeof e?e(t):s(s({},t),e)),n},u=function(e){var t=l(e.components);return a.a.createElement(c.Provider,{value:t},e.children)},d={inlineCode:"code",wrapper:function(e){var t=e.children;return a.a.createElement(a.a.Fragment,{},t)}},f=a.a.forwardRef((function(e,t){var n=e.components,r=e.mdxType,i=e.originalType,o=e.parentName,c=p(e,["components","mdxType","originalType","parentName"]),u=l(n),f=r,m=u["".concat(o,".").concat(f)]||u[f]||d[f]||i;return n?a.a.createElement(m,s(s({ref:t},c),{},{components:n})):a.a.createElement(m,s({ref:t},c))}));function m(e,t){var n=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var i=n.length,o=new Array(i);o[0]=f;var s={};for(var p in t)hasOwnProperty.call(t,p)&&(s[p]=t[p]);s.originalType=e,s.mdxType="string"==typeof e?e:r,o[1]=s;for(var c=2;c<i;c++)o[c]=n[c];return a.a.createElement.apply(null,o)}return a.a.createElement.apply(null,n)}f.displayName="MDXCreateElement"}}]);