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..20599f991daf6 --- /dev/null +++ b/examples/with-redux-toolkit-typescript/README.md @@ -0,0 +1,23 @@ +# Redux Toolkit TypeScript 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-typescript&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-typescript with-redux-toolkit-app +# or +yarn create next-app --example with-redux-toolkit-typescript 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}} +
+