This project was bootstrapped with Create React App. Tha aim of this repository is to get you started with React Hooks. Feel free to refer to the documentation or to the course materials in case of need.
For this project, you will need have:
- an IDE like VS Code
- an Browser like Google Chrome
- Node and npm installed. You can check this buy running
node -v
npm -v
- git locally installed.
We will be making a clone of the game called Cookie Clicker. In this game, the user gets a point (click) for every time he clicks the cookie. The user can use his clicks to buy some other bonuses. You will build a basic React project using functional components with Hooks that will help a user keep track of the clicks made on a button.
- Every 20 seconds, the user will get a bonus that makes every click count for
2x timesthe original click value for only5 seconds. - Meaning if the user click is originally
1, it will be counted for2, if it is4, it will count for8. - The user can use
100clicks from his clicks count to buy a bonus calledShadow clicker. Every shadow clicker will give the user a 1 click per second bonus.
You should create a fork of this repository under your github account and download it by running
$ git clone link_to_the_forked_repository
Run the following commands to start your project:
npm i
npm start
-
Remove everything that is inside the App component
-
Create a new directory called components. All our components are going to be created there.
-
Create a functional component called
Main. -
Import the component into the
Appcomponent and render it there. -
Start by creating the image of the cookie. You can bring one from the internet and creating.
-
Import
useStatefromreact. -
Create a state variable called
clicksusinguseStatehook. -
Use the
onClickevent listener to bind an event to make the cookie clickable and bind it to a function calledhandleClick -
Define the function handleClick inside your component. It should call
setClicksto increment it by 1 -
Create a new paragraph on top that ouputs the current score by rendering the value of
clicks -
Try the behaviour and make sure the value is being incremented by 1 with every click.
-
Clear any warnings caused by
eslint -
Now it's time to commit your changes with a message Implemented a simple click counter
$ git add .
$ git commit -m "Implemented a simple click counter"
$ git push
-
Create a new state variable called
stepwithuseStatehook and initialize it to1 -
Create a new paragraph under the score paragraph indicating the step
-
Update the handleClick
methodto increment theclicksvalue by the value ofstepin the state. -
Import
useEffecthook from react. -
Create a new effect with useEffect.
-
Use
setIntervalinside the effect to increment the value ofstepto2 * stepevery 20 seconds by callingsetStepfunction. -
Use
setTimeoutto make this change last for only5 seconds -
Now it's time to commit your changes with a message Implemented the automatic bonus
-
Create a new state variable called
shadowClickerswithuseStatehook and initialize it to0. -
Create a new paragraph under the step paragraph indicating the number of shadowClickers the user has bought.
-
Create a new effect that will update the value of
clicksby the number of shadow clickers (the value ofshadowClickers) every second. -
Create a new button to buy new shadow clickers.
-
Bind the button to a new function called
handleBuyShadowClicker. -
Implement the
handleBuyShadowClickermethod to:- remove 100 clicks of the current value of clicks
- increment the number of shadow clickers by 1.
-
Add a condition to the
handleBuyShadowClickermethod to only allow user to buy shadow clickers when he has enough balance. -
Test the behaviour and clear all the warnings in the console.
-
Now it's time to commit your changes with a message Implemented shadow clickers
If the user exists or refreshes the page, all his progress will be lost. That's why we will use the localstorage to persist all the values in our state
-
Call
localStorage.setItem()to save the new values whenever we have an update inside our effects and methods -
Create a new effect that
Only runs when we first visit the page -
Inside that effect, call
localstorage.getItem()to set our state variables to the values in the localstorageif they exist -
We can enhance this behaviour with a new effect that runs every 10 seconds to save all the current state of the game
-
Test the behaviour and clear all the warnings in the console.
-
Now it's time to commit your changes with a message Implemented state persistence
If you are done wih the features of the game, you can go ahead and refactor your styles and layouts to give a better experience to the players
You can submit your work by creating a Pull Request to the original repository under the name your_first_name you_last_name
- Class Material on Moodle
- ReactJS Documentation
- React useEffect Documentation
- React useState Documentation
- LocalStorage Documentation