Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update example with typescript #23249

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/with-redux-toolkit/declaration.d.ts
@@ -0,0 +1 @@
declare module '*.css'
11 changes: 10 additions & 1 deletion examples/with-redux-toolkit/package.json
Expand Up @@ -4,7 +4,8 @@
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
"start": "next start",
"type-check": "tsc"
},
"dependencies": {
"@reduxjs/toolkit": "1.5.0",
Expand All @@ -13,5 +14,13 @@
"react-dom": "17.0.1",
"react-redux": "7.2.2"
},
"devDependencies": {
"@types/node": "14.14.35",
"@types/react": "17.0.3",
"@types/react-dom": "17.0.2",
"@types/react-redux": "7.1.16",
"prettier": "2.2.1",
"typescript": "4.2.3"
},
"license": "MIT"
}
8 changes: 0 additions & 8 deletions examples/with-redux-toolkit/src/app/store.js

This file was deleted.

12 changes: 12 additions & 0 deletions examples/with-redux-toolkit/src/app/store.ts
@@ -0,0 +1,12 @@
import { combineReducers, configureStore } from '@reduxjs/toolkit'
import counterReducer from '../features/counter/counterSlice'

const rootReducer = combineReducers({
counter: counterReducer,
})

export type CoreState = ReturnType<typeof rootReducer>

export const store = configureStore({
reducer: rootReducer,
})
@@ -1,6 +1,11 @@
import { createSlice } from '@reduxjs/toolkit'
import { createSlice, Dispatch } from '@reduxjs/toolkit'
import { CoreState } from '../../app/store'

const initialState = {
type CounterState = {
value: number
}

const initialState: CounterState = {
value: 0,
}

Expand Down Expand Up @@ -31,7 +36,7 @@ export const { increment, decrement, incrementByAmount } = counterSlice.actions
// can be dispatched like a regular action: `dispatch(incrementAsync(10))`. This
// will call the thunk with the `dispatch` function as the first argument. Async
// code can then be executed and other actions can be dispatched
export const incrementAsync = (amount) => (dispatch) => {
export const incrementAsync = (amount: number) => (dispatch: Dispatch) => {
setTimeout(() => {
dispatch(incrementByAmount(amount))
}, 1000)
Expand All @@ -40,6 +45,6 @@ export const incrementAsync = (amount) => (dispatch) => {
// The function below is called a selector and allows us to select a value from
// the state. Selectors can also be defined inline where they're used instead of
// in the slice file. For example: `useSelector((state: RootState) => state.counter.value)`
export const selectCount = (state) => state.counter.value
export const selectCount = (state: CoreState) => state.counter.value

export default counterSlice.reducer
13 changes: 0 additions & 13 deletions examples/with-redux-toolkit/src/pages/_app.js

This file was deleted.

20 changes: 20 additions & 0 deletions examples/with-redux-toolkit/src/pages/_app.tsx
@@ -0,0 +1,20 @@
import { ComponentType, FC } from 'react'
import { Provider } from 'react-redux'
import { AppInitialProps } from 'next/app'
import { store } from '../app/store'
import '../styles/globals.css'

type AppProps = {
Component: ComponentType<AppInitialProps>
pageProps: AppInitialProps
}

const MyApp: FC<AppProps> = ({ Component, pageProps }) => {
return (
<Provider store={store}>
<Component {...pageProps} />
</Provider>
)
}

export default MyApp
23 changes: 23 additions & 0 deletions examples/with-redux-toolkit/tsconfig.json
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"allowJs": true,
"alwaysStrict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"jsx": "preserve",
"lib": ["dom", "es2017"],
"module": "esnext",
"moduleResolution": "node",
"noEmit": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "esnext"
},
"exclude": ["node_modules"],
"include": ["**/*.ts", "**/*.tsx"]
}