<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Amber M Webb</title>
    <description>The latest articles on DEV Community by Amber M Webb (@webb4web).</description>
    <link>https://dev.to/webb4web</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F10389%2FupwDgb9s.jpg</url>
      <title>DEV Community: Amber M Webb</title>
      <link>https://dev.to/webb4web</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/webb4web"/>
    <language>en</language>
    <item>
      <title>How I modernized an old React carousel app with hooks and styled-components</title>
      <dc:creator>Amber M Webb</dc:creator>
      <pubDate>Sat, 02 May 2020 17:57:24 +0000</pubDate>
      <link>https://dev.to/webb4web/how-i-modernized-an-old-react-carousel-app-with-hooks-and-styled-components-2gmn</link>
      <guid>https://dev.to/webb4web/how-i-modernized-an-old-react-carousel-app-with-hooks-and-styled-components-2gmn</guid>
      <description>&lt;p&gt;I am a front end developer. I have been writing front end applications since long before it was called front end. I do not remember how I was adding color and font weight to my HTML back in 2002. I also do not remember when I started using CSS but I do recall my first coding job where I came to hate it. There was no hot reloading back then, so any change that was made to the CSS I had to refresh the browser to see it. Then of course, I had to look in &lt;em&gt;&lt;strong&gt;ALL THE BROWSERS&lt;/strong&gt;&lt;/em&gt; (do not get me started on Internet Explorer 7) to make sure the interface looked like it was supposed to. I have had a few colleagues ask me the past few years how I felt about styled components. I had no answer for them except that I hate CSS and I want to go the easiest route to accomplish this necessary evil. I had no desire to learn styled components and would stick to class based styling or CSS modules thank you very much. One of the very best things about using frameworks like Bootstrap or Material UI is that the components are already styled out of the box. Sure yeah, you can customize the styling but you can also just plug them in, pass in your props and focus more on the UI interactions, the architecture and the performance of your application. You know, the stuff that makes coding so much fun. &lt;/p&gt;

&lt;p&gt;I recently revisited a little &lt;a href="https://github.com/amberwebb/another-react-carousel"&gt;React carousel application&lt;/a&gt; that I had built a couple of years ago. It had been sitting in my Github getting very stale and needed quite a bit of freshening up. It was using a much older version of React with class components. I have been riding the React hooks bandwagon for over a year, so it was time to convert it over. It felt awesome to pull out the constructors and lifecycle methods and replace with useState and useEffect hooks. &lt;/p&gt;

&lt;p&gt;Here is my original CarouselItem component using class component method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { Component } from 'react'
import PropTypes from 'prop-types'
import './Carousel.css'

class CarouselItem extends Component {
  static propTypes = {
    activeIndex: PropTypes.number.isRequired,
    index: PropTypes.number.isRequired,
    item: PropTypes.object.isRequired
  }

  constructor (props) {
    super(props)
    this.state = {
      className: props.index === props.activeIndex ? 'active' : ''
    }
  }

  componentWillReceiveProps (nextProps) {
    const {
      index
    } = this.props

    if (index === nextProps.activeIndex) {
      this.setState({className: 'active'})
    } else {
      this.setState({className: ''})
    }
  }

  render () {
    const { item } = this.props
    const { className } = this.state

    return (
      &amp;lt;li className={`Carousel-item ${className}`}&amp;gt;
        {item}
      &amp;lt;/li&amp;gt;
    )
  }
}

export default CarouselItem
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here is my CarouselItem component after updating it to use hooks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useEffect, useState } from 'react';
import './Carousel.css';

export default function CarouselItem(props) {
  const { index, activeIndex, item } = props;
  const [className, setClassName] = useState(props.index === props.activeIndex ? 'active' : '');

  useEffect(() =&amp;gt; {
    if (index === activeIndex) {
      setClassName('active');
    } else {
      setClassName('');
    }
  }, [index, activeIndex]);

  return (
    &amp;lt;li className={`Carousel-item ${className}`}&amp;gt;
      {item}
    &amp;lt;/li&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It was looking more modern already! But something was still not quite right. The ugly class names in my elements were bringing it down. Now, the styles are central to the way the carousel behaves. The active images have &lt;code&gt;diplay:block&lt;/code&gt; style and fade in to view and the little navigation controls change color when its respective image is active. I was setting the active class name in state in the useEffect hook. I decided it was time to give styled components a go.&lt;/p&gt;

&lt;p&gt;Styled components are very easy to use. Run this in your terminal:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install --save styled-components&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Add this to your React component:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import styled from 'styled-components'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Create a styled element like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MyAwesomStyledComponent = styled.div`
  background: red;
`
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Add it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;MyAwesomStyledComponent /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That is what I did! It felt great to delete the stylesheets. There was also an added benefit that was both surprising and delightful. Take a quick look again at my React hooks version of CarouselItem up above and then take a look at CarouselItem with styled components:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import styled, {keyframes} from 'styled-components';
import { CarouselItemInterface } from "./types";

const fadeIn = keyframes`
  from {
      opacity: 0;
  }
  to {
      opacity: 1;
  }
`;

const Item = styled.li`
  display: ${(props: CarouselItemInterface) =&amp;gt; props.index === props.activeIndex ? 'block' : 'none'};
  animation: ${fadeIn} 2s;
  -moz-animation: ${fadeIn} 2s;
  -webkit-animation: ${fadeIn} 2s;
  -o-animation: ${fadeIn} 2s;
`;

export default function CarouselItem(props: CarouselItemInterface) {
  return (
    &amp;lt;Item {...props}&amp;gt;
      {props.item}
    &amp;lt;/Item&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since props are passed into the styled component, I can do my conditional checking for &lt;code&gt;display: block&lt;/code&gt; or &lt;code&gt;display: none&lt;/code&gt; directly in it! I was able to remove both the useState and useEffect hooks for a much more streamlined component! &lt;/p&gt;

&lt;p&gt;My new thoughts on CSS - it is awesome with styled components. My examples are pretty basic and there is much more to learn and use with them. I look forward to creating new projects, and maybe even converting some of my older existing projects, to use styled components.&lt;/p&gt;

&lt;p&gt;Do yourself a favor and check it out : &lt;a href="https://styled-components.com/"&gt;https://styled-components.com/&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Create a D3 Bar Chart with Different Width Bars</title>
      <dc:creator>Amber M Webb</dc:creator>
      <pubDate>Sat, 08 Feb 2020 15:29:51 +0000</pubDate>
      <link>https://dev.to/webb4web/create-a-d3-bar-chart-with-different-width-bars-1a4c</link>
      <guid>https://dev.to/webb4web/create-a-d3-bar-chart-with-different-width-bars-1a4c</guid>
      <description>&lt;p&gt;Recently, I was asked to develop a bar chart in D3.js. No big deal, right? I have been writing D3 charts for about 6 months and have really enjoyed bringing data to life in applications for end users to consume. D3 has excellent documentation and even better examples that I have used to learn how to build these charts. The problem I ran into with this particular bar chart is the requirements were unlike the past charts. The bars would not be neatly and equally divided amongst the width of the chart and the x axis using scaleBand() function. These particular bars would have varying widths and the total of the bars would equal the max value of the x axis.&lt;/p&gt;

&lt;p&gt;I couldn't find any examples that made sense for me, so I tried some things on my own. I used a linear scale for the x axis and set the width of each bar, but the bars were overlapping. After some deep thought and Google-fu, I came across a StackOverflow post that helped get past my mind block. I have to calculate the x attribute of each bar! Aha!&lt;/p&gt;

&lt;p&gt;Instead of a lengthy explanation of how and why this works, here is my code example of a bar chart with bars of varying widths &lt;a href="https://github.com/amberwebb/d3-charts/blob/master/src/BarChart/index.js"&gt;https://github.com/amberwebb/d3-charts/blob/master/src/BarChart/index.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pay attention to the x attribute. This function takes all of the values (starting from 0) and calculates the x value of each bar. The bars retain their own widths, no overlapping and the total of the bars equals the max value of the x axis. Requirements met!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c_xTd2eA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pf8mlhchob2quk1ejpmz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c_xTd2eA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pf8mlhchob2quk1ejpmz.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
