Commit fc93eed9 authored by Mohak Trivedi's avatar Mohak Trivedi
Browse files

Activity 1 STUB created

parent b7b36317
import store from "../store";
// TODO: Import the store from store.js
import "./styles.css";
export default function App() {
const handleIncrement = () => {
store.dispatch({ type: "INCREMENT_CARS" });
// console.log(store.getState());
};
const handleDecrement = () => {
store.dispatch({ type: "DECREMENT_CARS" });
// console.log(store.getState());
};
const handleSetHundred = () => {
store.dispatch({ type: "SET_CARS", payload: { cars: 100 } });
};
// TODO: Log the store state and the cars from the store state.
const handleIncrement = () => {};
const handleDecrement = () => {};
return (
<div className="App">
......@@ -23,7 +20,6 @@ export default function App() {
<div className="controls">
<button onClick={handleIncrement}>+</button>
<button onClick={handleDecrement}>-</button>
<button onClick={handleSetHundred}>Set 100</button>
</div>
</div>
</div>
......
import { DECREMENT_CARS, INCREMENT_CARS, SET_CARS } from "./types";
export const incrementCars = () => {
return {
type: INCREMENT_CARS
};
};
export const decrementCars = () => {
return {
type: DECREMENT_CARS
};
};
export const setCars = (value) => {
return {
type: SET_CARS,
payload: { cars: value }
};
};
export const INCREMENT_CARS = "INCREMENT_CARS";
export const DECREMENT_CARS = "DECREMENT_CARS";
export const SET_CARS = "SET_CARS";
......@@ -14,4 +14,4 @@
.controls {
display: flex;
gap: 10px;
}
}
\ No newline at end of file
import { DECREMENT_CARS, INCREMENT_CARS, SET_CARS } from "./src/actions/types";
const initialState = {
cars: 0
};
const createStore = (reducer) => {
const createStore = () => {
const store = {};
store.state = initialState;
store.getState = () => {
return store.state;
};
// TODO: Implement the getState() such that it returns the state.
store.getState = () => {
}
store.dispatch = (action) => {
store.state = reducer(store.state, action);
console.log(store.getState());
//User to implement it in future milestones
store.incrementCount = () => {
};
return store;
};
const initialState = {
cars: 0
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case INCREMENT_CARS: {
const newState = { ...store.state, cars: store.state.cars + 1 };
return newState;
}
case DECREMENT_CARS: {
const newState = { ...store.state, cars: store.state.cars - 1 };
return newState;
}
case SET_CARS: {
const newState = { ...store.state, cars: action.payload.cars };
return newState;
}
default:
return state;
}
};
const store = createStore(reducer);
const store = createStore();
export default store;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment