From c484d14724262b0b5f449f5ca9478afe36c08839 Mon Sep 17 00:00:00 2001 From: Karst Date: Sun, 21 Mar 2021 18:10:41 +0100 Subject: [PATCH 1/2] feat: add exemple with redux toolkit in typescript --- .../with-redux-toolkit-typescript/.gitignore | 34 +++ .../with-redux-toolkit-typescript/README.md | 23 +++ .../components/add-note.tsx | 41 ++++ .../components/clock.tsx | 34 +++ .../components/counter.tsx | 156 ++++++++++++++ .../components/edit-note.tsx | 60 ++++++ .../lib/slices/clockSlice.ts | 29 +++ .../lib/slices/counterSlice.ts | 60 ++++++ .../lib/slices/notesSlice.ts | 193 ++++++++++++++++++ .../lib/useForm.ts | 24 +++ .../lib/useInterval.ts | 19 ++ .../next-env.d.ts | 2 + .../package.json | 27 +++ .../pages/_app.tsx | 21 ++ .../pages/api/notes.js | 112 ++++++++++ .../pages/index.tsx | 23 +++ .../pages/notes.tsx | 58 ++++++ .../src/store.ts | 18 ++ .../tsconfig.json | 24 +++ .../types/ErrorResponse.ts | 10 + .../types/Note.ts | 8 + 21 files changed, 976 insertions(+) create mode 100644 examples/with-redux-toolkit-typescript/.gitignore create mode 100644 examples/with-redux-toolkit-typescript/README.md create mode 100644 examples/with-redux-toolkit-typescript/components/add-note.tsx create mode 100644 examples/with-redux-toolkit-typescript/components/clock.tsx create mode 100644 examples/with-redux-toolkit-typescript/components/counter.tsx create mode 100644 examples/with-redux-toolkit-typescript/components/edit-note.tsx create mode 100644 examples/with-redux-toolkit-typescript/lib/slices/clockSlice.ts create mode 100644 examples/with-redux-toolkit-typescript/lib/slices/counterSlice.ts create mode 100644 examples/with-redux-toolkit-typescript/lib/slices/notesSlice.ts create mode 100644 examples/with-redux-toolkit-typescript/lib/useForm.ts create mode 100644 examples/with-redux-toolkit-typescript/lib/useInterval.ts create mode 100644 examples/with-redux-toolkit-typescript/next-env.d.ts create mode 100644 examples/with-redux-toolkit-typescript/package.json create mode 100644 examples/with-redux-toolkit-typescript/pages/_app.tsx create mode 100644 examples/with-redux-toolkit-typescript/pages/api/notes.js create mode 100644 examples/with-redux-toolkit-typescript/pages/index.tsx create mode 100644 examples/with-redux-toolkit-typescript/pages/notes.tsx create mode 100644 examples/with-redux-toolkit-typescript/src/store.ts create mode 100644 examples/with-redux-toolkit-typescript/tsconfig.json create mode 100644 examples/with-redux-toolkit-typescript/types/ErrorResponse.ts create mode 100644 examples/with-redux-toolkit-typescript/types/Note.ts diff --git a/examples/with-redux-toolkit-typescript/.gitignore b/examples/with-redux-toolkit-typescript/.gitignore new file mode 100644 index 0000000000000..1437c53f70bc2 --- /dev/null +++ b/examples/with-redux-toolkit-typescript/.gitignore @@ -0,0 +1,34 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# next.js +/.next/ +/out/ + +# production +/build + +# misc +.DS_Store +*.pem + +# debug +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# local env files +.env.local +.env.development.local +.env.test.local +.env.production.local + +# vercel +.vercel diff --git a/examples/with-redux-toolkit-typescript/README.md b/examples/with-redux-toolkit-typescript/README.md new file mode 100644 index 0000000000000..c0a6a3b9f3a0f --- /dev/null +++ b/examples/with-redux-toolkit-typescript/README.md @@ -0,0 +1,23 @@ +# Redux Toolkit example + +This example shows how to integrate Next.js with [Redux Toolkit](https://redux-toolkit.js.org). + +The **Redux Toolkit** is intended to be the standard way to write Redux logic (create actions and reducers, setup the store with some default middlewares like redux devtools extension). This example demonstrates each of these features with Next.js + +## Deploy your own + +Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example): + +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-redux-toolkit&project-name=with-redux-toolkit&repository-name=with-redux-toolkit) + +## How to use + +Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example: + +```bash +npx create-next-app --example with-redux-toolkit with-redux-toolkit-app +# or +yarn create next-app --example with-redux-toolkit with-redux-toolkit-app +``` + +Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)). diff --git a/examples/with-redux-toolkit-typescript/components/add-note.tsx b/examples/with-redux-toolkit-typescript/components/add-note.tsx new file mode 100644 index 0000000000000..c06d35bd43b3d --- /dev/null +++ b/examples/with-redux-toolkit-typescript/components/add-note.tsx @@ -0,0 +1,41 @@ +import { FC } from 'react' +import { useDispatch, useSelector } from 'react-redux' + +import { addNote, selectNotes } from '../lib/slices/notesSlice' +import useForm from '../lib/useForm' +import { Note } from '../types/Note' +import { isErrorResponse } from '../types/ErrorResponse' + +const AddNoteForm: FC = () => { + const dispatch = useDispatch() + const { error } = useSelector(selectNotes) + const handleSubmit = useForm({ + title: '', + content: '', + }) + + return ( +
dispatch(addNote(data)))}> +

Create a Note

+ +
+ {isErrorResponse(error) && {error.title}} +
+