Hacking with React

Welcome to Hacking with React, a hands-on introduction to making a web app using React 0.14, React Router 1.0, ES6, Jest and more. This book is brand new, so if you spot any typos or bugs, or if you just have general feedback, email or tweet me.

This e-book is available completely free online right here, but if you're able to purchase a copy of the book I would greatly appreciate your support. The two are identical in terms of their content, but the paid-for version is available as PDF, ePub and Mobi for a single purchase. This means you can read the book offline and on a variety of devices, plus you also support my work.

Buy the book for $10 and support my work – thank you!

If you scroll down you can find some frequently answered questions, or if you just want to dive right in go ahead and select the first chapter below. You might also be interested in my other free books, Hacking with Swift and Hacking with PHP.

The least you need to know: each chapter is very short, and designed to be read in order. Unless you know what you're doing, start with chapter 1 and work your way forward to the end.

Chapter index

  1. Begin at the Beginning: Chapter One
  2. The Importance of using Webpack with React
  3. Introduction to JSX
  4. Importing React Components using ES6
  5. What are React Props?
  6. Generating Random Values for Our Page
  7. How to Write if/else Conditional Statements in JSX
  8. Using JSX to Render Several Elements at Once
  9. Handling Events with JSX: onClick
  10. State vs Props in React
  11. Changing a React Component's State with setState()
  12. State and the Single Source of Truth
  13. Rendering an Array of Data with map() and JSX
  14. Cleaning up and Preparing for a Real Project
  15. Fetching Ajax Data from GitHub using SuperAgent
  16. Converting GitHub's JSON into Meaningful JSX
  17. Time for a Task: Reading from Three Feeds
  18. How to Upgrade Our App to Read Three Feeds
  19. Refactoring our Ajax Code: Don't Repeat Yourself
  20. Refactoring our State Code: Passing Parameters in onClick
  21. Introducing React Router
  22. How to Add a New Route to React Router
  23. Creating a Link Between Pages in React Router
  24. Making Custom URLs with React Router Params
  25. Adding a Root Route Using React Router and IndexRoute
  26. Cleaning up Our Routes and Preparing for the Next Step
  27. Time for a Test: Clickable Usernames
  28. Making Usernames Clickable: My Solution
  29. Time for some Basic User Interface Polish
  30. Adding React Router Breadcrumbs with Link and IndexLink
  31. The First 80% is Done: Now What?
  32. How to Configure Jest to Test React and ES6
  33. Creating our First React Test with Jest
  34. Using Jest to Test Our React Components
  35. Using Jest to Simulate User Interaction on a React Component
  36. Time for Ajax: Using Jest with Asynchronous Tests
  37. Mocking Ajax with Jest: Making an Asynchronous Test Become Synchronous
  38. Cleaning up Our Tests: Last Tweaks
  39. Linting React using ESLint and Babel
  40. Linting React using Airbnb's ESLint Rules
  41. How to Add React Component Prop Validation in Minutes
  42. Bringing it all Together: Project Complete!


Frequently asked questions

How much does this book cost? Hacking with React is available completely free at hackingwithreact.com. If you want to support my work (please do!) please consider buy the paid-for version of the book. The book has a suggested price of $12, but you can pay as low as $10 if you want, or *cough* more. Come to think of it, you do look like the generous sort…

Hey! It's asking me to pay more – what's going on? Depending on your location local taxes may apply to your purchase, which will increase the price you pay. As you might imagine, there's nothing I can do about this.

What will I learn? We'll cover lots of React, naturally. But we'll also be using ES6, Babel, Webpack, React Router, Jest, SuperAgent, Chance, ESLint and more.

Do you cover Flux or Redux? No, I’m afraid not. React has a steep learning curve all of its own even before you add in JSX, React Router, Jest and others; as much as I like Redux, adding it into the mix would have made things much too complicated for beginners. Perhaps if this book gets enough interest I shall be able to add Redux material as part of a second project.

Do I need to know ES6/ECMAScript 2015? No. This book assumes you're at least somewhat familiar with JavaScript, but I try to explain ES6 features along the way. This is not an ES6 tutorial, but at the same time I'm aware that many people haven't used it before so I do my best.

Wait… do web browsers even support ES6 yet? No, but we'll be using Babel to transpile ES6 to regular old JavaScript that works in any browser.

Will I actually make anything, or is it all just theory? This book takes you through one complete project that loads data using the GitHub API. I chose the GitHub API because it has public access to interesting data. In order to help make the learning curve shallow, the first quarter of the book uses very simple examples that evolve into the full project later on.

Will this book make me a React expert? No, I'm afraid not. Hacking with React is targeted at beginners, and walks you through creating a fairly simple example application while teaching the fundamentals of developing with React. If I get enough interest, it's possible I may add more projects in the future that go further.

How come this book is so short? I've seen React books twice this length! It's true: this is a short book, but it's short because I focus on teaching you React. I don't discuss the history of React or other components, and I don't go off on any tangents comparing various options. This book teaches you React in the shortest possible time, and I think you'll be impressed by how quickly you learn.

Does the book cover testing? Yes, although I leave it to near the end. Heresy, I know.

If I buy the paid-for version of the book do I get anything special? Yes, you get the warm fuzzy feeling of knowing you're helping keep my kids off the streets. Seriously: your support is hugely appreciated, and I'll high five you over the internet. (But otherwise: no, the paid-for version is identical to the web version.)

I spotted an error / typo – what should I do? Email me at paul@hackingwithreact.com and let me know. Please send me the chapter name that contains the error otherwise it can be hard to track down!

Can I get the source code for the finished project? Yup, it's all on GitHub.

Who are you? Why should I care what you say? My name is Paul Hudson, and my apps are used by the likes of MacLife magazine, Edge magazine, Fender, Virgin, Jamie Oliver, Odeon, Tesco, and more. I'm an author in my spare time, having written PHP in a Nutshell, Ubuntu Unleashed and Fedora Unleashed. Previously I was the editor of Linux Format magazine, but my writing has also appeared in MacFormat magazine, Net magazine and TechRadar.

You might like to know that I have two other "Hacking with…" books available online: Hacking with Swift teaching iOS coding and Hacking with PHP teaching server-side web development. Yes, they are both available online for free.

You will often find me on Stack Overflow answering questions about iOS, PHP, and React.

I love your stuff – are you available for hire? Get in touch at paul@hackingwithreact.com and let's talk – I love coding, and I love writing, so being able to put them both together is pure magic for me.

I have a question about something in the book. How can I contact you? Use paul@hackingwithreact.com if you want to email me. Alternatively, you should follow me on Twitter: @twostraws.

Copyright ©2016 Paul Hudson. Follow me: @twostraws.