Skip to content

Instantly share code, notes, and snippets.

View patwadeepak's full-sized avatar

Deepak Kumar Patwa patwadeepak

View GitHub Profile
@patwadeepak
patwadeepak / App.js
Created July 23, 2021 03:38
Pure React Way - App.js
import Posts from "./Components/Posts";
import PostForm from "./Components/PostForm";
import { useState } from "react";
const App = () => {
const [PostData, setPostData] = useState([]);
const addPost = (formData) => {
setPostData([formData, ...PostData]);
};
@patwadeepak
patwadeepak / PostForm.js
Created July 23, 2021 03:46
Pure React Way - PostForm.js
import { useState } from "react";
const PostForm = ({ addPost }) => {
const [formData, setFormData] = useState({
title: "",
body: "",
});
const handleChange = (ev) => {
setFormData({
@patwadeepak
patwadeepak / Post.js
Created July 23, 2021 03:47
Pure React Way - Post.js
import "../CSS/Post.css";
import { useEffect } from "react";
const Posts = ({ setPostData, PostData }) => {
useEffect(() => {
fetch("https://jsonplaceholder.typicode.com/posts")
.then((res) => res.json())
.then((data) => setPostData(data));
}, []);
@patwadeepak
patwadeepak / App.js
Created July 23, 2021 04:16
Redux Way - App.js
import { Provider } from "react-redux";
import store from "./store";
import Posts from "./Components/Posts";
import PostForm from "./Components/PostForm";
const App = () => (
<Provider store={store}>
<div className="app-container">
<h1>Blog Post App using Old ways of Redux</h1>
@patwadeepak
patwadeepak / PostForm.js
Created July 23, 2021 07:00
The Redux Way - PostForm.js
import "../CSS/PostForm.css";
import { useState } from "react";
import { connect } from "react-redux";
import { object } from "prop-types";
import { createNewPost } from "../actions/postActions";
const initialFormState = { title: "", body: "" };
const PostForm = ({ createNewPost }) => {
const [formData, setFormData] = useState(initialFormState);
@patwadeepak
patwadeepak / Post.js
Created July 23, 2021 07:04
The Redux Way - Post.js
import "../CSS/Post.css";
import { useEffect } from "react";
import { func, array, object } from "prop-types";
import { connect } from "react-redux";
import { fetchPosts } from "../actions/postActions";
const Posts = ({ posts, fetchPosts }) => {
useEffect(() => {
fetch("https://jsonplaceholder.typicode.com/posts")
.then((res) => res.json())
@patwadeepak
patwadeepak / index.js
Created July 23, 2021 07:09
The Redux Way - Boilerplate
// this is inside reducers folder. do not confuse this with react app's index.js
import { combineReducers } from "redux";
import postReducer from "./postReducer";
export default combineReducers({
posts: postReducer,
});
@patwadeepak
patwadeepak / App.js
Created July 23, 2021 07:22
The Context Way - All Files
import Posts from "./Components/Posts";
import PostForm from "./Components/PostForm";
import { useEffect, createContext, useReducer } from "react";
const appReducer = (state, action) => {
switch (action.type) {
case "FETCH_POSTS":
return [...state, ...action.payload];
case "NEW_POST":
return [action.payload, ...state];
@patwadeepak
patwadeepak / create-react-app.sh
Created March 25, 2022 14:19
CRA with redux toolkit template
npx create-react-app my-app --template redux