Setting up React.js Project with Redux in CodePen
CodePen Project: https://codepen.io/heretse/pen/xxabPJE
3 min readFeb 18, 2023
Pen Settings for JS
JavaScript Preprocessor
- Babel
Add External Scripts/Pens
- react
- react-dom
- redux
- react-redux
Pen Code
HTML Code:
<div id="root"></div>
CSS Code:
/**
* Credit
* counter desgin is from dribbble (https://dribbble.com/shots/5539678-Stepper-VI)
* chevron icons are from font-awesome (https://fontawesome.com/icons/chevron-down?style=solid)
**/
html,
body {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
background-color: #f4723a;
display: flex;
align-items: center;
justify-content: center;
}
.container {
display: flex;
align-items: center;
flex-direction: column;
}
.number {
font-size: 72px;
color: #e8e8e8;
font-family: Verdana;
line-height: 1.75em;
text-align: center;
user-select: none;
}
.chevron {
background: transparent center center;
background-repeat: no-repeat;
cursor: pointer;
width: 70px;
height: 70px;
transition: background-image 0.3s;
}
.chevron-up {
background-image: url("data:image/svg+xml, %3Csvg aria-hidden='true' focusable='false' role='img' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 448 512'%3E%3Cpath fill='%23f6909c' d='M240.971 130.524l194.343 194.343c9.373 9.373 9.373 24.569 0 33.941l-22.667 22.667c-9.357 9.357-24.522 9.375-33.901.04L224 227.495 69.255 381.516c-9.379 9.335-24.544 9.317-33.901-.04l-22.667-22.667c-9.373-9.373-9.373-24.569 0-33.941L207.03 130.525c9.372-9.373 24.568-9.373 33.941-.001z'%3E%3C/path%3E%3C/svg%3E");
}
.chevron-up:hover {
background-image: url("data:image/svg+xml, %3Csvg aria-hidden='true' focusable='false' role='img' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 448 512'%3E%3Cpath fill='%23f5b7b7' d='M240.971 130.524l194.343 194.343c9.373 9.373 9.373 24.569 0 33.941l-22.667 22.667c-9.357 9.357-24.522 9.375-33.901.04L224 227.495 69.255 381.516c-9.379 9.335-24.544 9.317-33.901-.04l-22.667-22.667c-9.373-9.373-9.373-24.569 0-33.941L207.03 130.525c9.372-9.373 24.568-9.373 33.941-.001z'%3E%3C/path%3E%3C/svg%3E");
}
.chevron-down {
background-image: url("data:image/svg+xml,%3Csvg aria-hidden='true' focusable='false' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 448 512'%3E%3Cpath fill='%23f6909c' d='M207.029 381.476L12.686 187.132c-9.373-9.373-9.373-24.569 0-33.941l22.667-22.667c9.357-9.357 24.522-9.375 33.901-.04L224 284.505l154.745-154.021c9.379-9.335 24.544-9.317 33.901.04l22.667 22.667c9.373 9.373 9.373 24.569 0 33.941L240.971 381.476c-9.373 9.372-24.569 9.372-33.942 0z'%3E%3C/path%3E%3C/svg%3E");
}
.chevron-down:hover {
background-image: url("data:image/svg+xml,%3Csvg aria-hidden='true' focusable='false' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 448 512'%3E%3Cpath fill='%23f5b7b7' d='M207.029 381.476L12.686 187.132c-9.373-9.373-9.373-24.569 0-33.941l22.667-22.667c9.357-9.357 24.522-9.375 33.901-.04L224 284.505l154.745-154.021c9.379-9.335 24.544-9.317 33.901.04l22.667 22.667c9.373 9.373 9.373 24.569 0 33.941L240.971 381.476c-9.373 9.372-24.569 9.372-33.942 0z'%3E%3C/path%3E%3C/svg%3E");
}
.visibility-hidden {
visibility: hidden;
}
JS Code:
// action
const increment = () => ({ type: "INCREMENT" });
const decrement = () => ({ type: "DECREMENT" });
// reducers
const initialState = { count: 0 };
const counterReducer = (state = initialState, action) => {
switch (action.type) {
case "INCREMENT":
return { ...state, count: state.count + 1 };
case "DECREMENT":
return { ...state, count: state.count - 1 };
default:
return state;
}
};
// store
const { createStore } = Redux;
const { Provider, useSelector, useDispatch } = ReactRedux;
const store = createStore(counterReducer);
const Counter = () => {
const count = useSelector((state) => state.count);
const dispatch = useDispatch();
const handleIncrement = () => {
dispatch(increment());
};
const handleDecrement = () => {
dispatch(decrement());
};
return (
<div className="container">
<div className={`chevron chevron-up ${count >= 10 && "visibility-hidden"}`} onClick={handleIncrement}></div>
<div className="number">{count} </div>
<div className={`chevron chevron-down ${count <= 0 && "visibility-hidden"}`} onClick={handleDecrement}></div>
</div>
);
};
const rootNode = document.getElementById("root");
const root = ReactDOM.createRoot(rootNode);
root.render(
<Provider store={store}>
<div style={{ display: "flex", justifyContent: "space-between" }}>
<Counter />
</div>
</Provider>
);
The results:
How to login the CodePen
- Go to the CodePen website at https://codepen.io/.
- Click the “Sign Up” button in the top right corner of the page.
- Fill out the registration form with your email address, username, and password, and click “Create Account”.
- You will receive an email with a verification link. Click the link to verify your account.
- Once your account is verified, you can log in to CodePen using your email address and password.
- Click the “Log In” button in the top right corner of the page.
- Enter your email address and password, and click “Log In”.