A basic game of Hangman written in React using Create React App scaffolding.
Visit http://rh.oviprojects.xyz/ to play the game.
There are no bells and whistles to be found here. Consider this project as an experiment in restraint, and as a good tutorial for developing in React without additional libraries.
- This project does not adhere to the
CSS-in-JSmovement, so there's noradium,glamor,styled-components,uranium,smart-css, oraphordite. - The language level is
ES6withJSX, so nopublic class fields, nofunction bind syntax, nozones, noObservable, nodecoratorsor other stage 0 up to 2 features. I loveBabelas much as anyone, but I'd like someone reading this project 4 years from now to still understand the syntax. - Because of
create-react-appI don't directly usewebpack, at least on the surface, because under the hood that's exactly what the project is built with. Not having to write the configuration file lets me concentrate on the code. - In addition there is no
Redux,Flux,Flummox,Alt,Fluxxor,MartyJS,McFly,Fluxible,Delorean(kinda sensing a theme here),Lux,RefluxorFluxy. State management libraries are insanely great, but only if you need them. - I could have used a backend but I'm not building an MMO, so I decided against it. Obviously that means there's no
Next.js. - I like to think JavaScript has come a long way since I first started using it circa 2005, so I choose not to use:
TypeScript,CoffeeScript,LiveScriptorElm, though I'd love to writeElmone day. create-react-appuses only CSS so there's noLESS,SASSorStylus.- And since I'm using
ES6I didn't needlodash,underscoreorRamda.
- Q: Why did you write
react-hangman? - A: Because I love games and they're fun to write.
- Q: Still, this game is very simple. Why write it at all?
- A: It was a learning experience. Even though I've been writing
Reactfor almost a year, I still found new things to learn while writing this game. - Q: Is it really possible to write a web app in 2017 using only
React? - A: Yes.
- Q: But, you're using
React15, why didn't you use stateless components?! - A: The real question is, had I did, would my code have been any better for it?
- Q: Yes.
- A: Why?
- Q: Because stateless components are better.
- A: ...
- Q: How did you manage state without Redux?!
- A: By using local
Reactstate. - Q: What's that?
- A: It's
React's built-in state management system. Each component can store arbitrary data and pass it down to its children viaprops. - Q: That sounds very difficult. You could've at least used
FluxifReduxis too hard for you to understand. - A: You don't need state management for a hangman game.
- Q: No, I think you don't understand
Redux. It's so much faster than anything else. - A: Fast at what?
- Q: At everything.
- A: Next question.
- Q: I can't find your
webpack.config.jsfile, where is it? - A: I'm using the excellent create-react-app project and it takes care of that for you.
- Q: So how do you update the
webpackconfig? - A: I don't.
- Q: Are you using
webpack2? - A: No.
- Q: Why not?
- A: How would it help me in a hangman game?
- Q:
webpack2just got released. - A: Is that a question?
- Q: How did you bundle your app?
- A:
yarn build - Q: So cool that you're using
yarn. It's so much faster thannpm! - A: Um.. yeah.
yarnis pretty cool. - Q: The game's bundle size is huge! Can you make it smaller?
- A: The only reason why the bundle has a large file size is because I embedded an-array-of-english-words, which is a pretty big dependency. Had I used a backend, I would've saved some KB, at the expense of network latency, of course.
- Q: So.. can you make the bundle smaller?
- A: No.
- Q: What other dependencies did you use?
- A: You could check
package.json, you know. The only dependencies arecreate-react-app,an-array-of-english-words, andnode-uuid. - Q:
node-uuid? - A: I needed a way to generate consistently unique keys for the components that make up the word during the game. The only entropy I had was the letter, but a word can have the same letter more than once, so I used
node-uuidto generate a unique identifier. - Q: How did you write CSS? I thought browsers were already running
LESSnatively?! - A: Browsers only support CSS... There's no support for anything other than CSS in create-react-app, and, frankly, I'm glad they didn't add it.
- Q: You mean you don't know
LESS? - A: I do know how to write
LESSbut for a simple app it doesn't bring much value. - Q: It is better, though. You should learn it.
- A: You should teach me...
- Q: How is the hangman drawn?
- A: With
SVG. - Q:
SVGis finished. Why didn't you useHTML5 Canvas? - A: ...it's just a man hanging from a stick. Why would I use the
CanvasAPI for that? - Q:
SVGis.. - A: Yeah, you know what? I used
Canvasafter all. - Q: What library did you use for the stick man?
- A: I just rendered it with the S..
CanvasAPI with inspiration from another Hangman project. - Q: Are you using
Jestto test your app? - A: Although I'm a strong advocate of unit-tests this game is simple enough that I didn't care enough to write tests for it.
- Q:
Mochathen? - A: There are no tests in the project, except for a default one generated by
create-react-app. - Q: But how will you accept pull requests without tests?
- A: I won't be accepting any PRs. The game is done.
- Q: You mean your game is not open-source?
- A: No, I mean the game is finished. I'm not taking it any further.
- Q: How/where did you host the bundle?
- A: Surge.
- Q: How can I get your project from
npm? - A: This project is not on
npmand it won't be published. It's not a library, it's just a sample project on how to useReactto write a fairly basic game. - Q: What IDE did you use to write the source code?
- A: Atom with some plugins for
BabelandESLint. - Q: Why didn't you...
- A: Please stop.
MIT