Skip to content

Instantly share code, notes, and snippets.

@justinobney
Last active February 5, 2017 01:27
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinobney/5c952ffedf51a61b8192 to your computer and use it in GitHub Desktop.
Save justinobney/5c952ffedf51a61b8192 to your computer and use it in GitHub Desktop.

Learn React (or other libs)

Let's get together and build a component in React each week. We can then check out each other's solutions to the challenge and hopefully get better at this development thing. While my goal is to get better at React, other libraries are also welcome. There is so much out there we can learn from.

This is not meant to be a tutorial. There are TONS of those for react already on the web. We are going to start with very simple components and work into more complex scenarios.

Some good resources:

I have also provided a very simple JSBin starting template.

We are still working out how we will showcase our solutions to make collaboration and viewing easier. For the time being, feel free to post links to you JSBins in the comments of this Gist

Week 1

This week I picked a component from a Dribbble shot. The main focus is on the simple component itself. I have added a little rendering logic at the bottom of the JSBin starting template. You can mainly ignore this. We will eventually get into state management, but I wanted to start simple.

The component (outlined in red)

Small number Input Component

More Info

Should accept the following props:

  • label - text next to value
  • value - numeric value to render
  • onChange - function to call with +1 or -1 (intent to change value)
  • icon? - this could be an optional icon

Perhaps something like:

<SimpleNumberInput
  value={guestCount}
  label="Guests"
  onChange={handleGuestChange}
  icon="user" />

Submissions:

Week 2

The component should have the following characteristics:

  • render a list of items
  • each item should be able to be in "view" or "edit" mode

The ability to add a new item is optional, but beyond the scope of this exercise. The goal of this exercise is rendering lists of data and swapping the rendered components based on state.

The (suggested) component concept

Inline Edt List

Example state:

const state = {
  items: [
    {id: 1, name: "let's learn react", url:'http://facebook.github.io/react/', category:'tech'},
    {id: 2, name: "by building things", url:'https://github.com/enaqx/awesome-react', category:'tech'},
    {id: 3, name: "it could be fun", url:'https://gist.github.com/justinobney/5c952ffedf51a61b8192', category:'tech'}
  ],
  editId: null
}

Submissions:

@justinobney
Copy link
Author

Week 1 (SimpleNumberInput) - http://jsbin.com/geduvik/edit?js,output

@petebrowne
Copy link

http://jsbin.com/fegitiraye/1/edit?js,output

  • Uses stateless function
  • Adds PropTypes
  • Adds min/max props

@justinobney
Copy link
Author

Awesome @petebrowne! I initially went stateless, but wanted a createClass example for newer devs. But that is exactly what I was looking to gain by having other developers chime in with their implementations.

@aclave1
Copy link

aclave1 commented Jan 8, 2016

week 1:
http://jsbin.com/laveqedetu/1/edit?html,css,js,output

stole pete's idea and called render inside of my increment function since it's stateless.

@aclave1
Copy link

aclave1 commented Jan 14, 2016

week 2:

my first pass at it. i might come back and add some better styles later but this is "learn react", not "learn css" right?

http://jsbin.com/ribopigome/7/edit?css,js,output

I do another version that dynamically generates the inputs based on a sort order map

@justinobney
Copy link
Author

week 2:
using refs callbacks to get info about input value

http://jsbin.com/sohumiy/5/edit?js,output

@petebrowne
Copy link

A little late, but here we go: http://jsbin.com/tedulafeca/edit?js,output

  • This allows the user to cancel the edits and revert to previous state
  • While editing one item, you the other "Edit" buttons are disabled so you don't inadvertently lose your edits

@thatgibbyguy
Copy link

Hey guys, @petebrowne shared this with us at LocalMed. My comments would be better shared here, and I'll do my best to contribute as things progress.

My first thought is who are these challenges for? If it's for experienced devs, right on. If it's for newer devs the pace might be too quick. For example consider the gap in what is covered here http://reactfordesigners.com/labs/reactjs-introduction-for-people-who-know-just-enough-jquery-to-get-by/ compared to week 1 to week 2 on this project.

Either way, great concept and like I said, I'll look to contribute when I can. But I'll certainly be following you guys' ideas.

@samfarr2
Copy link

I was going to catch up because React looks interesting, but I'm afraid my js skills are not up to par. I may need to crawl a while longer before I can run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment