Site icon C1CTech

ReactJS|Props

<p>What if we want to pass data from one component to another that&&num;8217&semi;s because we don&&num;8217&semi;t want the component to display static data instead we want to pass dynamic data to the component and render it over the webpage&period; To achieve this React comes with <span style&equals;"color&colon; &num;0000ff&semi;"><strong>props<&sol;strong><&sol;span>&period; Basically props are used to pass data from component to component&period; In this article&comma; We will talk about React props in greater detail&period;<&sol;p>&NewLine;<p><span class&equals;"embed-youtube" style&equals;"text-align&colon;center&semi; display&colon; block&semi;"><amp-youtube data-videoid&equals;"LxjfQ2TPt4c" data-param-rel&equals;"1" data-param-showsearch&equals;"0" data-param-showinfo&equals;"1" data-param-iv&lowbar;load&lowbar;policy&equals;"1" data-param-fs&equals;"1" data-param-hl&equals;"en-US" data-param-autohide&equals;"2" data-param-wmode&equals;"transparent" width&equals;"1200" height&equals;"675" layout&equals;"responsive"><a href&equals;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;watch&quest;v&equals;LxjfQ2TPt4c" placeholder><amp-img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;LxjfQ2TPt4c&sol;hqdefault&period;jpg" alt&equals;"YouTube Poster" layout&equals;"fill" object-fit&equals;"cover"><noscript><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;LxjfQ2TPt4c&sol;hqdefault&period;jpg" loading&equals;"lazy" decoding&equals;"async" alt&equals;"YouTube Poster"><&sol;noscript><&sol;amp-img><&sol;a><&sol;amp-youtube><&sol;span><&sol;p>&NewLine;<h3 id&equals;"react-props" class&equals;"chapter-header"><strong><span style&equals;"color&colon; &num;000080&semi;">What are Props in React&quest;<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>Normally you start out with React’s <strong><span style&equals;"color&colon; &num;0000ff&semi;">JSX<&sol;span><&sol;strong> syntax for rendering something to the browser when learning about React&period; Basically&comma; JSX mixes HTML with JavaScript to get the best of both worlds&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1047" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;05&sol;prop11n-239937493-1558688837554&period;png" alt&equals;"prop11n" width&equals;"793" height&equals;"460" &sol;><&sol;p>&NewLine;<p>On splitting <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Welcome<&sol;strong><&sol;span> component separately it looks something like this as shown below &colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1048" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;05&sol;prop12n-627078166-1558689264684&period;png" alt&equals;"prop12n&period;png" width&equals;"857" height&equals;"593" &sol;><&sol;p>&NewLine;<p>Now&comma; the question is <span style&equals;"color&colon; &num;008000&semi;"><strong>how to pass the data from one React component to another component&lpar;<span style&equals;"color&colon; &num;000000&semi;">ie&period; from App to Welcome component<&sol;span>&rpar;&quest;<&sol;strong> <span style&equals;"color&colon; &num;000000&semi;">React allows us to pass information to a Component using something called <span style&equals;"color&colon; &num;0000ff&semi;"><strong>props<&sol;strong><&sol;span> &lpar;stands for properties&rpar;&period; Props&comma; in particular&comma; is a javascript object which contains all the properties that you have defined while component invocation inside the parent component&period;<&sol;span><&sol;span><&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Passing and Accessing props<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>You can pass props to a component &lpar;irrespective of its type&rpar; in the same way you define  HTML attributes&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>class-based component<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>To access the props inside <span style&equals;"color&colon; &num;000000&semi;">class component <&sol;span>we make use of <strong><span style&equals;"color&colon; &num;0000ff&semi;">this &period;props<&sol;span>&period;<&sol;strong>The &OpenCurlyQuote;this&period;props’ is a kind of global object which stores all of a component&&num;8217&semi;s props&period; The <strong>propName<&sol;strong><em>&lpar;ie&period; <strong><span style&equals;"color&colon; &num;0000ff&semi;">welcome<&sol;span><&sol;strong>&rpar;<&sol;em>&comma; is the key of this object with value <span style&equals;"color&colon; &num;0000ff&semi;"><strong>&&num;8216&semi;Welcome to React&&num;8217&semi;<&sol;strong><&sol;span>&period; You can see in the below code&colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1049" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;05&sol;prop13n-1014711639-1559128315718&period;png" alt&equals;"prop13n" width&equals;"867" height&equals;"595" &sol;><&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>function-based component<&sol;strong> <&sol;span><&sol;h4>&NewLine;<p>In a <span style&equals;"color&colon; &num;0000ff&semi;"><strong>functional component<&sol;strong><&sol;span>&comma; the props are defined in the same way we defined props for the class component but to access the props firstly&comma; we have to add props parameter and then to read the props we make use of <span style&equals;"color&colon; &num;0000ff&semi;"><strong>props<&sol;strong><&sol;span> instead of<span style&equals;"color&colon; &num;0000ff&semi;"><strong> this&period;props<&sol;strong><&sol;span> &period; As shown in below code&colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1050" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;05&sol;prop14n-1933235088-1559128346267&period;png" alt&equals;"prop14n" width&equals;"824" height&equals;"520" &sol;><&sol;p>&NewLine;<p>Since you will find always the props in the function signature&comma; which most of the time is only the container object of your data&comma; but not the data to be used&comma; you can <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;mozilla&period;org&sol;en-US&sol;docs&sol;Web&sol;JavaScript&sol;Reference&sol;Operators&sol;Destructuring&lowbar;assignment" target&equals;"&lowbar;blank" rel&equals;"noopener noreferrer">destructure<&sol;a><&sol;strong><&sol;span> the props early in the function signature&period; One would call it React props destructuring&colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1061" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;05&sol;prop&period;png" alt&equals;"prop" width&equals;"684" height&equals;"105" &sol;><&sol;p>&NewLine;<p>As you have seen&comma; props enable you to pass variables from one to another component down the component tree&period; In the previous example&comma; it was only a string variable&period; But props can be anything i&period;e integers&comma; objects&comma; arrays and even React components&period;<&sol;p>&NewLine;<p>You can also define the props inline&period; In case of strings&comma; you can pass props inside double quotes &lpar;or single quotes&rpar; too as in the below code&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1051" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;05&sol;prop15n-1780605649-1559131944257&period;png" alt&equals;"prop15n" width&equals;"823" height&equals;"470" &sol;><&sol;p>&NewLine;<p>But you can also pass other data structures with inline props&period; In case of objects&comma; it can be confusing for React beginners&comma; because you have two curly braces&colon; one for the JSX and one for the object&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1052" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;05&sol;prop16n-1091350802-1559131971505&period;png" alt&equals;"prop16n" width&equals;"926" height&equals;"510" &sol;><&sol;p>&NewLine;<p>Basically&comma; that’s how props are passed to React components&period; As you may have noticed&comma; props are only passed from top to bottom in React’s component tree&period; There is no way to pass props up to a parent component&period;  There is a limitation with props that is Props are read-only&period; We are not allowed to modify the content of a prop&period; Whatever the type of Component is – functional or class-based&comma; none of them is allowed to modify their props&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;99 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<p><img class&equals;"alignnone size-full wp-image-1060" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;05&sol;prop24n-1903979977-1559132369180&period;png" alt&equals;"prop24n" width&equals;"865" height&equals;"821" &sol;><&sol;p>&NewLine;<p>In the above code if you will try to change the <strong><span style&equals;"color&colon; &num;0000ff&semi;">subject<&sol;span><&sol;strong> prop with some another value &&num;8216&semi;Redux&&num;8217&semi; then it will give an error&period;<&sol;p>&NewLine;<h3 id&equals;"react-props-syntax-spread-rest" class&equals;"chapter-header"><span style&equals;"color&colon; &num;000080&semi;"><strong>React &&num;8230&semi;props syntax<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>Another strategy for passing all props to a child component is the <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;mozilla&period;org&sol;en-US&sol;docs&sol;Web&sol;JavaScript&sol;Reference&sol;Operators&sol;Spread&lowbar;syntax" target&equals;"&lowbar;blank" rel&equals;"noopener noreferrer">JavaScript spread operator<&sol;a><&sol;strong><&sol;span>&period; JavaScript’s spread operator in React is a useful power feature and you can read people referring to it as the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>React …props syntax<&sol;strong><&sol;span> even though it is not really a React feature but just a thing coming from JavaScript&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1053" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;05&sol;prop17n-1477935187-1559132011818&period;png" alt&equals;"prop17n" width&equals;"1038" height&equals;"815" &sol;><&sol;p>&NewLine;<p>The props spreading can be used to spread a whole object with key-value pairs down to a child component&period; It has the same effect as passing each value of the object by its own to the component as before&period; The child component gets the props the same way as before too&period; Another thing which builds up on top of the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>prop spread<&sol;strong><&sol;span> is the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>prop spread with rest<&sol;strong>&period;<&sol;span><&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1054" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;05&sol;prop18n-3750730908-1559132053444&period;png" alt&equals;"prop18n" width&equals;"1053" height&equals;"819" &sol;><&sol;p>&NewLine;<p>As you can see&comma; in the <strong>Welcome<&sol;strong> component the props are destructured but with a <strong>rest assignment<&sol;strong> which is called <strong><span style&equals;"color&colon; &num;0000ff&semi;">other<&sol;span><&sol;strong> in this case&period; So you have the <strong><span style&equals;"color&colon; &num;0000ff&semi;">subject<&sol;span><&sol;strong> prop but also the <strong><span style&equals;"color&colon; &num;0000ff&semi;">other<&sol;span><&sol;strong> prop which is essentially just an object with all the remaining properties &lpar;in this case only the <strong><span style&equals;"color&colon; &num;0000ff&semi;">description<&sol;span><&sol;strong>&rpar;&period; Now you can spread the rest of the props to the Description component&comma; because all the relevant props for the other components &lpar;here the Title component&rpar; were separated from it&period;<&sol;p>&NewLine;<h3 id&equals;"react-props-default-value" class&equals;"chapter-header"><strong><span style&equals;"color&colon; &num;000080&semi;">React props with default value<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>In some cases&comma; you may want to pass default values as props&period; Historically the best approach to it was using JavaScript’s logical OR operator&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1055" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;05&sol;prop19n-3331632605-1559132117206&period;png" alt&equals;"prop19n" width&equals;"695" height&equals;"332" &sol;><&sol;p>&NewLine;<p>You can also inline it as a prop&colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1056" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;05&sol;prop20n-353413325-1559132151990&period;png" alt&equals;"prop20n" width&equals;"787" height&equals;"318" &sol;><&sol;p>&NewLine;<p>However&comma; with JavaScript language additions there are other features you can use for it&period; For instance <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;mozilla&period;org&sol;en-US&sol;docs&sol;Web&sol;JavaScript&sol;Reference&sol;Functions&sol;Default&lowbar;parameters" target&equals;"&lowbar;blank" rel&equals;"noopener noreferrer">JavaScript’s default parameter<&sol;a><&sol;span><&sol;strong> for the default value of the prop&colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1057" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;05&sol;prop21n-202086796-1559132218352&period;png" alt&equals;"prop21n" width&equals;"810" height&equals;"293" &sol;><&sol;p>&NewLine;<p>The latter is the most popular choice when using only JavaScript for the default value&period; However&comma; in case you are using <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;reactjs&period;org&sol;docs&sol;typechecking-with-proptypes&period;html" target&equals;"&lowbar;blank" rel&equals;"noopener noreferrer">React’s PropTypes<&sol;a><&sol;span><&sol;strong>&comma; it’s is also possible to pass <span style&equals;"color&colon; &num;0000ff&semi;"><strong>default prop values<&sol;strong><&sol;span> the React way&colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1058" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;05&sol;prop22n-656853583-1559132273545&period;png" alt&equals;"prop22n" width&equals;"799" height&equals;"366" &sol;><&sol;p>&NewLine;<p>After all&comma; my recommendation would be using JavaScript’s default parameter&comma; because everyone outside of the React world understands it as well&period;<&sol;p>&NewLine;<h3 id&equals;"react-props-children" class&equals;"chapter-header"><strong><span style&equals;"color&colon; &num;000080&semi;">React&&num;8217&semi;s children prop<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>The children prop in React can be used for composing React components into each other&period; Rather than using inheritance &lpar;due to the nature of React’s class components&rpar;&comma; React <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;reactjs&period;org&sol;docs&sol;composition-vs-inheritance&period;html" target&equals;"&lowbar;blank" rel&equals;"noopener noreferrer">embraces composition over inheritance<&sol;a><&sol;span><&sol;strong>&period; That’s why you can just put any string&comma; element&lpar;s&rpar; or React component&lpar;s&rpar; in between of the opening and closing component tags&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1059" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;05&sol;prop23n-1044249358-1559132318287&period;png" alt&equals;"prop23n" width&equals;"760" height&equals;"836" &sol;><&sol;p>&NewLine;<p>In this case&comma; only the div element is put in between the component tags&period; Then in the child component&comma; you can make use of everything which is in between of the tags &lpar;string&comma; element&lpar;s&rpar;&comma; component&lpar;s&rpar;&rpar;&comma; by using <strong>React’s children prop<&sol;strong>&period; For instance&comma; you can just render the content of the children prop like it is done in the above example&period;<&sol;p>&NewLine;<p>In the end&comma; props aren’t difficult&period; They are read-only and enable you to pass data down the component tree&period; Now&comma; what if we want to manage some data inside the component that might change over time&period; To achieve this React comes with <strong><span style&equals;"color&colon; &num;0000ff&semi;">state<&sol;span><&sol;strong> &period; In the next article we will talk about state&period; So I hope this article gave you a good overview of the different usages of props in React&period;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version