See the original app live at pixelartcss
This exercise will get you familiar with Redux actions, reducers composition and mapping state to views. All you should care about is the model and the state (you shouldn't worry about the view layer, it's already there).
Get comfortable with javascript.
Clone this repo, then install node packages and run:
git clone https://github.com/Ner1Co/pixel-art-react.git
cd pixel-art-react
npm install
npm run developmentThe app will run at http://localhost:8080 with hot reloading.
If you don't have npm installed:
brew install nodeUnder store directory, you can find actions.js for actions constants, actionCreators.js for all the action creators, reducer.js for all the reducers and selectors.js for state selectors.
Feel free to move or change everything as you like, you also can jump between different parts of the exercise.
All the React views are under the components directory.
Enjoy!
The app is loaded with a snapshot of initial state, we love cats - but we also want to start our own new draw.
There is an action called NEW_PROJECT, and newProject action creator:
- In
NewProject.jsxcomponent file, map the dispatchingNEW_PROJECTaction to the button click event. The functionmapDispatchToPropsmust return an object with aonClickkey (the component input), and a value that is a function with no parameters that dispatch your action.
{ onClick: () => dispatch(yourActionObject) }
Now, on every New button click, an action is dispatched to the reducer. You can see all the logged actions in the browser console or with redux-dev-tools extension.
- Handle
NEW_PROJECTaction in your root reducer and return a new state. You can use the helper functionnewProjectfor getting a new state with default values.
Suggested solution.
Clicking on the grid is already mapped to an action creator called cellClicked in the PixelCanvas.jsx component.
- Create a corresponding action and handle this action in the reducer. Return new state when the clicked cell in the active frame is filled with color. Remember: The state is immutable, use spread operator, and map.
- Use reducers composition in order to handle the
framespiece of state separately.
Note: Copying the frame grid on each draw, may not the best solution - it's only for simplicity.
Read more about Persistent data structures and Immutable.js and Performance.
Suggested solution.
This step is a good example of how different reducers can handle the same action.
-
Create
changeActiveFrameandcreateNewFrameaction creators and corresponding actions, then handle the actions in yourframesandactiveFrameIndexreducers. The action creators are already connected to theFrameHandler.jsxcomponent. Use the helper functions from/utils/frame.js. -
(Extra) Do the same for
deleteFrame,duplicateFrame,changeFrameInterval,changeDimensionsandresetGridaction creators.
Note: In this stage the reducer file become larger and you are encouraged to read this, and refactor your code shape. Remember: action creators and reducers are just functions, you can do any change that suits your case.
Congratulations! if you have finished this part, you can create an animation from multiple frames.
Add functionality for color selection, tools selection, erasing and color picking.
Suggested solution.
Reducers and action creators are easy to test, feel free to write your own tests under test directory. Make sure your file have _spec suffix, and run:
npm run testMIT Copyright © 2016 Javier Valencia Romero (@jvalen)