{"id":7233,"date":"2019-06-23T13:00:56","date_gmt":"2019-06-23T13:00:56","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=7233"},"modified":"2019-06-23T13:00:56","modified_gmt":"2019-06-23T13:00:56","slug":"react-and-css","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/react-and-css\/","title":{"rendered":"React and CSS"},"content":{"rendered":"<p>There are several ways to use CSS within React applications.<\/p>\n<p><strong>Inline CSS<\/strong><\/p>\n<p>We can create a structural type as a variable or const (more likely), for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nconst style = {\r\n  background: 'red',\r\n  width: '100px',\r\n  color: 'white'\r\n};\r\n\r\nexport const MyStyledDiv: React.SFC&lt;{}&gt; = ({\r\n}) =&gt; &lt;div style={style}&gt;Hello World&lt;\/div&gt;\r\n<\/pre>\n<p>In this case we&#8217;re using React.SFC just to reduce the code to a minimal, but fully fledged Components would work in the same way within the render function.<\/p>\n<p><strong>Style based components<\/strong><\/p>\n<p>With style based components we in essence create a component from the style using the back tick syntax along with the prefix styled.{element type}, so for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nconst DivStyle = styled.div`\r\n  background: red;\r\n  width: 100px;\r\n  color: white;\r\n`;\r\n\r\nexport const MyStyledDiv: React.SFC&lt;{}&gt; = ({\r\n}) =&gt; &lt;DivStyle&gt;Hello World&lt;\/DivStyle&gt;\r\n<\/pre>\n<p><strong>CSS Modules<\/strong><\/p>\n<p>We can create separate files for our css (known as <a href=\"https:\/\/facebook.github.io\/create-react-app\/docs\/adding-a-css-modules-stylesheet\" rel=\"noopener noreferrer\" target=\"_blank\">CSS Module StyleSheets<\/a>).<\/p>\n<p>In this case we create a .css file, for example MyStyledDiv.module.css which uses standard CSS syntax, so for example here my file<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\n.myDivStyle {\r\n    background-color: red;\r\n    width: 100px;\r\n    color: white;\r\n}\r\n<\/pre>\n<p>Now in our component we import the CSS in much the same way as importing components, for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport styles from '.\/MyStyledDiv.module.css';\r\n\r\nexport const MyStyledDiv: React.SFC&lt;{}&gt; = ({\r\n}) =&gt; &lt;div className={styles.myDivStyle}&gt;Hello World&lt;\/div&gt;\r\n<\/pre>\n<p>These style sheets are locally scoped to the component which imports them which helps us to reduce possible name clashes etc.<\/p>\n<p><strong>Standard CSS files<\/strong><\/p>\n<p>In this case we have standard CSS files, here we have a file named myStyleDiv.css with the following (which is the same as our module.css contents)<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\n.myDivStyle {\r\n    background-color: red;\r\n    width: 100px;\r\n    color: white;\r\n}\r\n<\/pre>\n<p>Now we again import the CSS like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport '.\/MyStyledDiv.css';\r\n\r\nexport const MyStyledDiv: React.SFC&lt;{}&gt; = ({\r\n}) =&gt; &lt;div className=&quot;myDivStyle&quot;&gt;Hello World&lt;\/div&gt;\r\n<\/pre>\n<p><strong>Emotion<\/strong><\/p>\n<p>We can also use third party library&#8217;s such as <a href=\"https:\/\/emotion.sh\/docs\/introduction\" rel=\"noopener noreferrer\" target=\"_blank\">emotion<\/a>.<\/p>\n<p>Here&#8217;s the same example using emotion<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nconst DivStyle = styled.div(props =&gt; ({\r\n  background: 'red',\r\n  width: '100px',\r\n  color: 'white'\r\n}))\r\n\r\nexport const MyStyledDiv: React.SFC&lt;{}&gt; = ({\r\n}) =&gt; &lt;DivStyle&gt;Hello World&lt;\/DivStyle&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There are several ways to use CSS within React applications. Inline CSS We can create a structural type as a variable or const (more likely), for example const style = { background: &#8216;red&#8217;, width: &#8216;100px&#8217;, color: &#8216;white&#8217; }; export const MyStyledDiv: React.SFC&lt;{}&gt; = ({ }) =&gt; &lt;div style={style}&gt;Hello World&lt;\/div&gt; In this case we&#8217;re using React.SFC [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[243],"tags":[],"class_list":["post-7233","post","type-post","status-publish","format-standard","hentry","category-react"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=7233"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7233\/revisions"}],"predecessor-version":[{"id":7244,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7233\/revisions\/7244"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=7233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=7233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=7233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}