Commit daa95de6 authored by marsandheart's avatar marsandheart

路由和全局变量整理

parent 6abf43ec
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
"build": "react-scripts build", "build": "react-scripts build",
"test": "react-scripts test", "test": "react-scripts test",
"eject": "react-scripts eject", "eject": "react-scripts eject",
"prt": "node prt.js" "lint": "node prt.js"
}, },
"eslintConfig": { "eslintConfig": {
"extends": "react-app" "extends": "react-app"
......
import React from 'react'; import React from 'react';
import logo from './logo.svg'; import { Provider } from 'react-redux';
import './App.css'; import store from './store/index';
import Router from './Router';
function App() { function App() {
return ( return (
<div className="App"> <Provider store={store}>
<header className="App-header"> <Router />
<img src={logo} className="App-logo" alt="logo" /> </Provider>
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
); );
} }
......
...@@ -2,11 +2,7 @@ import React from 'react'; ...@@ -2,11 +2,7 @@ import React from 'react';
import './App.css'; import './App.css';
function App() { function App() {
return ( return <div className="App-header">友邦加保项目 预览版</div>;
<div className="App-header">
友邦加保项目 预览版
</div>
);
} }
export default App; export default App;
import React from 'react';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import DoubleRecord from './pages/DoubleRecord';
import InsuredInfo from './pages/InsuredInfo';
export default function Routers() {
return (
<Router>
<Switch>
<Route exact path="/">
<InsuredInfo />
</Route>
<Route path="/doubleRecord">
<DoubleRecord />
</Route>
</Switch>
</Router>
);
}
import React from 'react'; import React from 'react';
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import './index.css'; import './index.css';
import Main from './Main'; import App from './App';
import * as serviceWorker from './serviceWorker'; import * as serviceWorker from './serviceWorker';
ReactDOM.render(<Main />, document.getElementById('root')); ReactDOM.render(<App />, document.getElementById('root'));
// If you want your app to work offline and load faster, you can change // If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls. // unregister() to register() below. Note this comes with some pitfalls.
......
// 这里可以看作 将原始redux中的action reducer都放入了同一个文件,
// reducers和effects都可作为方法给组件调用,区别在于effects中可以写较为复杂逻辑和异步方法,reducers中为纯函数
const count = {
state: 0,
reducers: {
add(state, payload) {
return state + payload;
},
double(state) {
return state * 2;
}
},
effects: {
async addAsync(payload, rootState) {
await new Promise((resolve) => setTimeout(resolve, 1000));
this.add(payload);
},
async doubleAsync(payload, rootState) {
await new Promise((resolve) => setTimeout(resolve, 1000));
this.double();
},
async addThenDoubleAsync(payload, rootState) {
await this.addAsync(payload);
await this.doubleAsync();
}
}
};
export default count;
import { init } from '@rematch/core';
import count from './count';
const store = init({
models: {
count
}
});
export default store;
import React from 'react';
import { connect } from 'react-redux';
const Count = (props) => (
<div>
<div>the count is {props.count}</div>
<div>
<div>
<button onClick={props.add}>add</button>加
</div>
<div>
<button onClick={props.double}>double</button>加
</div>
<div>
<button onClick={props.addAsync}>addAsync</button>一秒钟后加
</div>
<div>
<button onClick={props.doubleAsync}>doubleAsync</button>一秒钟后加
</div>
<div>
<button onClick={props.addThenDoubleAsync}>addThenDoubleAsync</button>异步加一然后异步加
各一秒钟
</div>
</div>
</div>
);
const mapState = (state) => ({
count: state.count
});
const mapDispatch = ({ count: { add, double, addAsync, doubleAsync, addThenDoubleAsync } }) => ({
add: () => add(1),
double,
addAsync: () => addAsync(1),
doubleAsync,
addThenDoubleAsync: () => addThenDoubleAsync(1)
});
export default connect(mapState, mapDispatch)(Count);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment