Skip to content

Commit

Permalink
refactor(basic-routes-rematch-new):用ts重新案例
Browse files Browse the repository at this point in the history
  • Loading branch information
SunLxy committed Apr 8, 2022
1 parent 8ffd0ad commit 8f222fa
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ ReactDOM.hydrate(
</BrowserRouter>
</Provider>
, document.getElementById('root'));

// @ts-ignore
if (module.hot) {
// @ts-ignore
module.hot.accept();
}
13 changes: 0 additions & 13 deletions example/basic-routes-rematch-new/src/models/demo.js

This file was deleted.

14 changes: 14 additions & 0 deletions example/basic-routes-rematch-new/src/models/demo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createModel } from "@rematch/core"
import { RootModel } from "./index"

export default createModel<RootModel>()({
name: "demo",
state: {
title: "demo 标题"
},
effects: () => ({
async very(_, store) {
console.log("打印 demo", store.demo.title)
}
})
})
13 changes: 0 additions & 13 deletions example/basic-routes-rematch-new/src/models/index.js

This file was deleted.

36 changes: 36 additions & 0 deletions example/basic-routes-rematch-new/src/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import login from "./login"
import demo from "./demo"
// @ts-ignore
import {
init,
Models,
Model,
RematchRootState,
RematchDispatch,
} from '@rematch/core';
import loading, { ExtraModelsFromLoading } from '@rematch/loading';

export const models = {
login,
demo,
}

export interface RootModel extends Models<RootModel> {
login: typeof login,
demo: typeof demo,
}
export type FullModel = ExtraModelsFromLoading<RootModel>

export const store = init<RootModel, FullModel>({
models,
plugins: [loading()],
});

export const { dispatch, addModel } = store;
export type Store = typeof store;
export type AddModel = typeof addModel;
export type Dispatch = RematchDispatch<RootModel>;
export type RootState = RematchRootState<RootModel, FullModel>;
export type ModelDefault<T = any> = Model<RootModel, T>;

export default store;
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { createModel } from "@rematch/core"
import { RootModel } from "./index"

export default createModel()({
export default createModel<RootModel>()({
name: "login",
state: {
title: "login 标题"
},
effects: () => ({
async very(_, { login }) {
console.log("打印 login", login.title)
async very(_, store) {
console.log("打印 login", store.login.title)
}
}),
reducers: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import React from "react";
import { useSelector } from 'react-redux'

import { RootState } from "./../../models"
const About = () => {
const title = useSelector(({ demo }) => demo.title)

const title = useSelector<RootState>((store) => store.demo.title)
return <div>About {title}</div>
}
export default About;
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from "react";
import { useSelector } from 'react-redux'
import { RootState } from "./../../models"

const Home = () => {
const title = useSelector(({ login }) => login.title)
const title = useSelector<RootState>(({ login }) => login.title)

const [count, setCount] = React.useState(1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const Nav = () => {
export default () => {
return <Routes>
<Route path="/" element={<Nav />} >
<Route index path="/home" element={<Home />} />
<Route index element={<Home />} />
<Route path="/home" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="*" element={<div>无页面</div>} />
</Route>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ const logs = console.log; // eslint-disable-line

const server = http.createServer(app);
let currentApp = app;
const PORT = parseInt(process.env.PORT || 3000) + 1;
const PORT = parseInt(process.env.PORT || "3000") + 1;
const HOST = process.env.HOST || 'localhost';
// @ts-ignore
server.listen(PORT, (error) => {
if (error) {
logs(error);
}
logs('🚀 started!', `PORT: http://${HOST}:${PORT}`);
});

// @ts-ignore
if (module.hot) {
logs('✅ Server-side HMR Enabled!');
// @ts-ignore
module.hot.accept('./serverIndex', () => {
logs('🔁 HMR Reloading `./serverIndex`...');
server.removeListener('request', currentApp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import { Provider } from 'react-redux';
import store from "./models"
import App from './routes';


// @ts-ignore
const assetsMainifest = new Function(`return ${FS.readFileSync(`${OUTPUT_PUBLIC_PATH}/asset-client-manifest.json`, "utf-8")}`)()


const appDirectory = FS.realpathSync(process.cwd());
const resolveApp = (relativePath) => Path.resolve(appDirectory, relativePath);
const resolveApp = (relativePath: string) => Path.resolve(appDirectory, relativePath);
const server = express();
// server.use(express.static(resolveApp("dist")))

const render = (props = {}) => {
const render = (props: any = {}) => {
const html = renderToString(
<Provider store={store}>
<StaticRouter location={props.url}>
Expand Down
33 changes: 33 additions & 0 deletions example/basic-routes-rematch-new/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"compilerOptions": {
"target": "esnext",
"lib": [
"esnext",
"dom"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"declaration": true,
"baseUrl": ".",
"jsx": "react-jsx",
"noFallthroughCasesInSwitch": true,
"noEmit": true,
"paths": {
"@/*": [
"./src/*"
],
}
},
"include": [
"src/**/*",
".kktssrrc.ts"
]
}
2 changes: 1 addition & 1 deletion example/basic-routes/src/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BrowserRouter } from "react-router-dom"
// import { BrowserRouter } from "react-router"
import App from './routes/index';

ReactDOM.render(<BrowserRouter><App /></BrowserRouter>, document.getElementById('root'));
ReactDOM.hydrate(<BrowserRouter><App /></BrowserRouter>, document.getElementById('root'));

// @ts-ignore
if (module.hot) {
Expand Down
6 changes: 5 additions & 1 deletion example/basic-routes/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const server = http.createServer(app);
let currentApp = app;
const PORT = parseInt(process.env.PORT || "3000") + 1;
const HOST = process.env.HOST || 'localhost';
server.listen(PORT, () => {
// @ts-ignore
server.listen(PORT, (error) => {
if (error) {
logs(error);
}
logs('🚀 started!', `PORT: http://${HOST}:${PORT}`);
});
// @ts-ignore
Expand Down
5 changes: 1 addition & 4 deletions example/basic-routes/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@
"@/*": [
"./src/*"
],
"@@/*": [
"./src/.uiw/*"
]
}
},
"include": [
"src/**/*",
".kktrc.ts"
".kktssrrc.ts"
]
}

0 comments on commit 8f222fa

Please sign in to comment.