└── 16期 ├── lesson1 ├── README.md ├── config-overrides.js ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ ├── index.html │ ├── logo192.png │ ├── logo512.png │ ├── manifest.json │ └── robots.txt ├── src │ ├── App.js │ ├── components │ │ ├── Dialog.js │ │ └── kFormCreate.js │ ├── index.css │ ├── index.js │ └── pages │ │ ├── DialogPage.js │ │ ├── FormPage.js │ │ ├── FormPage2.js │ │ ├── HocPage.js │ │ └── MyFormPage.js └── yarn.lock └── lesson2-save ├── .gitignore ├── README.md ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── src ├── App.js ├── index.css ├── index.js ├── kRedux.js ├── pages │ ├── ConsumerPage.js │ ├── ContextPage.js │ ├── ContextTypePage.js │ ├── MultipleContextsPage.js │ └── ReduxPage.js ├── store │ └── index.js ├── themeContext.js └── userContext.js └── yarn.lock /16期/lesson1/README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `yarn start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `yarn test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `yarn build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `yarn eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | 46 | ### Code Splitting 47 | 48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 49 | 50 | ### Analyzing the Bundle Size 51 | 52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 53 | 54 | ### Making a Progressive Web App 55 | 56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 57 | 58 | ### Advanced Configuration 59 | 60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 61 | 62 | ### Deployment 63 | 64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 65 | 66 | ### `yarn build` fails to minify 67 | 68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 69 | -------------------------------------------------------------------------------- /16期/lesson1/config-overrides.js: -------------------------------------------------------------------------------- 1 | //根目录创建config-overrides.js 2 | const { 3 | override, 4 | fixBabelImports, 5 | addDecoratorsLegacy 6 | } = require("customize-cra"); 7 | 8 | module.exports = override( 9 | fixBabelImports("import", { 10 | //antd按需加载 11 | libraryName: "antd", 12 | libraryDirectory: "es", 13 | style: "css" 14 | }), 15 | addDecoratorsLegacy() //配置装饰器 16 | ); 17 | -------------------------------------------------------------------------------- /16期/lesson1/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lesson1", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^4.2.4", 7 | "@testing-library/react": "^9.3.2", 8 | "@testing-library/user-event": "^7.1.2", 9 | "antd": "^3.26.11", 10 | "react": "^16.12.0", 11 | "react-dom": "^16.12.0", 12 | "react-scripts": "3.4.0" 13 | }, 14 | "scripts": { 15 | "start": "react-app-rewired start", 16 | "build": "react-app-rewired build", 17 | "test": "react-app-rewired test", 18 | "eject": "react-app-rewired eject" 19 | }, 20 | "eslintConfig": { 21 | "extends": "react-app" 22 | }, 23 | "browserslist": { 24 | "production": [ 25 | ">0.2%", 26 | "not dead", 27 | "not op_mini all" 28 | ], 29 | "development": [ 30 | "last 1 chrome version", 31 | "last 1 firefox version", 32 | "last 1 safari version" 33 | ] 34 | }, 35 | "devDependencies": { 36 | "@babel/plugin-proposal-decorators": "^7.8.3", 37 | "babel-plugin-import": "^1.13.0", 38 | "customize-cra": "^0.9.1", 39 | "react-app-rewired": "^2.1.5" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /16期/lesson1/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bubucuo/kkb-github/5d6a2d37a92b0f0f335e17be827571f05085f89c/16期/lesson1/public/favicon.ico -------------------------------------------------------------------------------- /16期/lesson1/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /16期/lesson1/public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bubucuo/kkb-github/5d6a2d37a92b0f0f335e17be827571f05085f89c/16期/lesson1/public/logo192.png -------------------------------------------------------------------------------- /16期/lesson1/public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bubucuo/kkb-github/5d6a2d37a92b0f0f335e17be827571f05085f89c/16期/lesson1/public/logo512.png -------------------------------------------------------------------------------- /16期/lesson1/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /16期/lesson1/public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /16期/lesson1/src/App.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import HocPage from "./pages/HocPage"; 3 | import FormPage from "./pages/FormPage"; 4 | import FormPage2 from "./pages/FormPage2"; 5 | import MyFormPage from "./pages/MyFormPage"; 6 | import DialogPage from "./pages/DialogPage"; 7 | 8 | function App() { 9 | return ( 10 |
11 | {/* 高阶组件 */} 12 | {/* */} 13 | 14 | {/* 表单组件 */} 15 | {/* */} 16 | 17 | {/* 表单组件 使用create */} 18 | {/* */} 19 | 20 | {/* 自己实现一个create */} 21 | {/* */} 22 | 23 | {/* 弹窗*/} 24 | 25 |
26 | ); 27 | } 28 | 29 | export default App; 30 | -------------------------------------------------------------------------------- /16期/lesson1/src/components/Dialog.js: -------------------------------------------------------------------------------- 1 | import React, {Component} from "react"; 2 | import {createPortal} from "react-dom"; 3 | 4 | export default class Dialog extends Component { 5 | constructor(props) { 6 | super(props); 7 | const doc = window.document; 8 | this.node = doc.createElement("div"); 9 | doc.body.appendChild(this.node); 10 | } 11 | 12 | componentWillUnmount() { 13 | window.document.body.removeChild(this.node); 14 | } 15 | 16 | render() { 17 | return createPortal( 18 |
19 |

Dialog

20 | {this.props.children} 21 |
, 22 | this.node 23 | ); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /16期/lesson1/src/components/kFormCreate.js: -------------------------------------------------------------------------------- 1 | import React, {Component} from "react"; 2 | 3 | export default function kFormCreate(Cmp) { 4 | return class extends Component { 5 | constructor(props) { 6 | super(props); 7 | this.state = {errors: {}}; 8 | this.options = {}; 9 | } 10 | handleChange = e => { 11 | // setState name value 12 | let {name, value} = e.target; 13 | // this.setState({[name]: value}); 14 | this.validate({ 15 | ...this.state, 16 | [name]: value 17 | }); 18 | }; 19 | getFieldDecorator = (field, option) => { 20 | this.options[field] = option; 21 | return InputCmp => { 22 | // !克隆一份 23 | return ( 24 |
25 | {React.cloneElement(InputCmp, { 26 | name: field, 27 | value: this.state[field] || "", 28 | onChange: this.handleChange 29 | })} 30 |

{this.state.errors[field]}

31 |
32 | ); 33 | 34 | // return ( 35 | // 41 | // ); 42 | 43 | // !createElement 44 | // return React.createElement(InputCmp.type, { 45 | // ...InputCmp.props, 46 | // name: field, 47 | // value: this.state[field] || "", 48 | // onChange: this.handleChange 49 | // }); 50 | }; 51 | }; 52 | getFieldsValue = () => { 53 | return {...this.state}; 54 | }; 55 | getFieldValue = field => { 56 | return this.state[field]; 57 | }; 58 | validate = state => { 59 | // 校验错误信息 60 | const errors = {}; 61 | // const state = {...this.state}; 62 | for (let name in this.options) { 63 | if (state[name] === undefined) { 64 | // 没有输入,判断为不合法 65 | errors[name] = this.options[name].rules[0].message; //"error"; 66 | } 67 | } 68 | this.setState({...state, errors}); 69 | }; 70 | validateFields = callback => { 71 | // 校验错误信息 72 | // const errors = {}; 73 | const state = {...this.state}; 74 | this.validate(state); 75 | 76 | // for (let name in this.options) { 77 | // if (state[name] === undefined) { 78 | // // 没有输入,判断为不合法 79 | // errors[name] = this.options[name].rules[0].message; //"error"; 80 | // } 81 | // } 82 | // this.setState({errors}); 83 | const {errors} = state; 84 | if (JSON.stringify(errors) === "{}") { 85 | // 合法 86 | callback(undefined, state); 87 | } else { 88 | callback(errors, state); 89 | } 90 | }; 91 | render() { 92 | return ( 93 |
94 | 100 |
101 | ); 102 | } 103 | }; 104 | } 105 | -------------------------------------------------------------------------------- /16期/lesson1/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 4 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | padding: 8px; 9 | } 10 | 11 | code { 12 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", 13 | monospace; 14 | } 15 | 16 | .border { 17 | margin: 4px; 18 | padding: 4px; 19 | border: solid red 1px; 20 | } 21 | 22 | .greenBorder { 23 | margin: 4px; 24 | padding: 4px; 25 | border: solid green 1px; 26 | } 27 | 28 | .red { 29 | line-height: 20px; 30 | color: red; 31 | } 32 | 33 | .dialog { 34 | position: absolute; 35 | top: 0; 36 | right: 0; 37 | bottom: 0; 38 | left: 0; 39 | line-height: 30px; 40 | width: 400px; 41 | height: 300px; 42 | transform: translate(50%, 50%); 43 | border: solid 1px gray; 44 | text-align: center; 45 | } -------------------------------------------------------------------------------- /16期/lesson1/src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import "./index.css"; 4 | import App from "./App"; 5 | 6 | ReactDOM.render(, document.getElementById("root")); 7 | -------------------------------------------------------------------------------- /16期/lesson1/src/pages/DialogPage.js: -------------------------------------------------------------------------------- 1 | import React, {Component} from "react"; 2 | import Dialog from "../components/Dialog"; 3 | 4 | export default class DialogPage extends Component { 5 | constructor(props) { 6 | super(props); 7 | this.state = { 8 | showDialog: false 9 | }; 10 | } 11 | render() { 12 | const {showDialog} = this.state; 13 | return ( 14 |
15 |

DialogPage

16 | 22 | {showDialog && ( 23 | 24 |

我是一段文本

25 |
26 | )} 27 |
28 | ); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /16期/lesson1/src/pages/FormPage.js: -------------------------------------------------------------------------------- 1 | import React, {Component} from "react"; 2 | import {Form, Input, Icon, Button} from "antd"; 3 | 4 | export default class FormPage extends Component { 5 | constructor(props) { 6 | super(props); 7 | this.state = { 8 | name: "", 9 | password: "" 10 | }; 11 | } 12 | submit = () => { 13 | console.log("submit", this.state); //sy-log 14 | }; 15 | render() { 16 | const {name, password} = this.state; 17 | return ( 18 |
19 |

FormPage

20 |
21 | 22 | 26 | this.setState({ 27 | name: e.target.value 28 | }) 29 | } 30 | /> 31 | 32 | 33 | 38 | this.setState({ 39 | password: e.target.value 40 | }) 41 | } 42 | /> 43 | 44 | 47 |
48 |
49 | ); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /16期/lesson1/src/pages/FormPage2.js: -------------------------------------------------------------------------------- 1 | import React, {Component} from "react"; 2 | import {Form, Input, Icon, Button} from "antd"; 3 | 4 | const nameRules = {required: true, message: "please input ur name"}; 5 | const passwordRules = {required: true, message: "please input ur password"}; 6 | 7 | @Form.create({}) 8 | class FormPage2 extends Component { 9 | submit = () => { 10 | const {getFieldsValue, getFieldValue, validateFields} = this.props.form; 11 | validateFields((err, values) => { 12 | if (err) { 13 | console.log("err", err); //sy-log 14 | } else { 15 | console.log("success", values); //sy-log 16 | } 17 | }); 18 | // console.log("submit", getFieldsValue(), getFieldValue("name")); //sy-log 19 | }; 20 | render() { 21 | console.log("props", this.props); //sy-log 22 | const {getFieldDecorator} = this.props.form; 23 | return ( 24 |
25 |

FormPage2

26 |
27 | 28 | {getFieldDecorator("name", {rules: [nameRules]})( 29 | 30 | )} 31 | 32 | 33 | {getFieldDecorator("password", {rules: [passwordRules]})( 34 | 35 | )} 36 | 37 | 40 |
41 |
42 | ); 43 | } 44 | } 45 | 46 | export default FormPage2; 47 | -------------------------------------------------------------------------------- /16期/lesson1/src/pages/HocPage.js: -------------------------------------------------------------------------------- 1 | import React, {Component} from "react"; 2 | 3 | //HOC: 是个函数,接收一个组件,返回一个新的组件 4 | // function Child(props) { 5 | // return
Child
; 6 | // } 7 | // Cmp这里是function或者class组件 8 | const foo = Cmp => props => { 9 | return ( 10 |
11 | 12 |
13 | ); 14 | }; 15 | const foo2 = Cmp => props => { 16 | return ( 17 |
18 | 19 |
20 | ); 21 | }; 22 | 23 | // 从下往上 24 | @foo2 25 | @foo 26 | @foo 27 | class Child extends Component { 28 | render() { 29 | return
Child
; 30 | } 31 | } 32 | 33 | // const foo = Cmp => { 34 | // return props => { 35 | // return ( 36 | //
37 | // 38 | //
39 | // ); 40 | // }; 41 | // }; 42 | 43 | // const Foo = foo2(foo(foo(Child))); 44 | export default class HocPage extends Component { 45 | render() { 46 | return ( 47 |
48 |

HocPage

49 | {/* */} 50 | 51 |
52 | ); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /16期/lesson1/src/pages/MyFormPage.js: -------------------------------------------------------------------------------- 1 | import React, {Component} from "react"; 2 | import kFormCreate from "../components/kFormCreate"; 3 | 4 | const nameRules = {required: true, message: "please input ur name"}; 5 | const passwordRules = {required: true, message: "please input ur password"}; 6 | 7 | @kFormCreate 8 | class MyFormPage extends Component { 9 | submit = () => { 10 | const {getFieldsValue, getFieldValue, validateFields} = this.props; 11 | validateFields((err, values) => { 12 | if (err) { 13 | console.log("err", err); //sy-log 14 | } else { 15 | console.log("success", values); //sy-log 16 | } 17 | }); 18 | console.log("submit", getFieldsValue(), getFieldValue("password")); 19 | }; 20 | render() { 21 | console.log("props", this.props); //sy-log 22 | const {getFieldDecorator} = this.props; 23 | return ( 24 |
25 |

MyFormPage

26 | {getFieldDecorator("name", {rules: [nameRules]})( 27 | 28 | )} 29 | {getFieldDecorator("password", {rules: [passwordRules]})( 30 | 31 | )} 32 | 33 |
34 | ); 35 | } 36 | } 37 | 38 | export default MyFormPage; 39 | -------------------------------------------------------------------------------- /16期/lesson2-save/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /16期/lesson2-save/README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `yarn start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `yarn test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `yarn build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `yarn eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | 46 | ### Code Splitting 47 | 48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 49 | 50 | ### Analyzing the Bundle Size 51 | 52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 53 | 54 | ### Making a Progressive Web App 55 | 56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 57 | 58 | ### Advanced Configuration 59 | 60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 61 | 62 | ### Deployment 63 | 64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 65 | 66 | ### `yarn build` fails to minify 67 | 68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 69 | -------------------------------------------------------------------------------- /16期/lesson2-save/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lesson2", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^4.2.4", 7 | "@testing-library/react": "^9.3.2", 8 | "@testing-library/user-event": "^7.1.2", 9 | "antd": "^3.26.11", 10 | "react": "^16.12.0", 11 | "react-dom": "^16.12.0", 12 | "react-redux": "^7.2.0", 13 | "react-scripts": "3.4.0", 14 | "redux": "^4.0.5", 15 | "redux-logger": "^3.0.6", 16 | "redux-thunk": "^2.3.0" 17 | }, 18 | "scripts": { 19 | "start": "react-scripts start", 20 | "build": "react-scripts build", 21 | "test": "react-scripts test", 22 | "eject": "react-scripts eject" 23 | }, 24 | "eslintConfig": { 25 | "extends": "react-app" 26 | }, 27 | "browserslist": { 28 | "production": [ 29 | ">0.2%", 30 | "not dead", 31 | "not op_mini all" 32 | ], 33 | "development": [ 34 | "last 1 chrome version", 35 | "last 1 firefox version", 36 | "last 1 safari version" 37 | ] 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /16期/lesson2-save/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bubucuo/kkb-github/5d6a2d37a92b0f0f335e17be827571f05085f89c/16期/lesson2-save/public/favicon.ico -------------------------------------------------------------------------------- /16期/lesson2-save/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /16期/lesson2-save/public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bubucuo/kkb-github/5d6a2d37a92b0f0f335e17be827571f05085f89c/16期/lesson2-save/public/logo192.png -------------------------------------------------------------------------------- /16期/lesson2-save/public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bubucuo/kkb-github/5d6a2d37a92b0f0f335e17be827571f05085f89c/16期/lesson2-save/public/logo512.png -------------------------------------------------------------------------------- /16期/lesson2-save/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /16期/lesson2-save/public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /16期/lesson2-save/src/App.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ContextPage from "./pages/ContextPage"; 3 | import ReduxPage from "./pages/ReduxPage"; 4 | 5 | function App() { 6 | return ( 7 |
8 | {/* 上下文 */} 9 | {/* */} 10 | 11 | {/* Redux */} 12 | 13 |
14 | ); 15 | } 16 | 17 | export default App; 18 | -------------------------------------------------------------------------------- /16期/lesson2-save/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 4 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | padding: 10px; 9 | } 10 | 11 | code { 12 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", 13 | monospace; 14 | } 15 | 16 | .border { 17 | margin: 10px 0; 18 | padding: 4px; 19 | border: 1px solid red; 20 | } 21 | 22 | .red { 23 | color: red; 24 | } 25 | 26 | .green { 27 | color: green; 28 | } 29 | 30 | .pink { 31 | color: pink; 32 | } 33 | -------------------------------------------------------------------------------- /16期/lesson2-save/src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import "./index.css"; 4 | import App from "./App"; 5 | 6 | ReactDOM.render(, document.getElementById("root")); 7 | -------------------------------------------------------------------------------- /16期/lesson2-save/src/kRedux.js: -------------------------------------------------------------------------------- 1 | export function createStore(reducer) { 2 | let currentState = undefined; 3 | let currentListeners = []; 4 | 5 | function getState() { 6 | return currentState; 7 | } 8 | function subscribe(listener) { 9 | currentListeners.push(listener); 10 | } 11 | function dispatch(action) { 12 | currentState = reducer(currentState, action); 13 | currentListeners.map(v => v()); 14 | return action; 15 | } 16 | 17 | dispatch({type: "INIT/KKB-REDUX"}); 18 | return {getState, dispatch, subscribe}; 19 | } 20 | -------------------------------------------------------------------------------- /16期/lesson2-save/src/pages/ConsumerPage.js: -------------------------------------------------------------------------------- 1 | import React, {Component} from "react"; 2 | import {ThemeConsumer} from "../themeContext"; 3 | 4 | export default class ConsumerPage extends Component { 5 | render() { 6 | return ( 7 |
8 |

ConsumerPage

9 | {ctx => } 10 |
11 | ); 12 | } 13 | } 14 | 15 | function HandleTabBar({themeColor}) { 16 | console.log("themeColor", themeColor); //sy-log 17 | return
文本
; 18 | } 19 | -------------------------------------------------------------------------------- /16期/lesson2-save/src/pages/ContextPage.js: -------------------------------------------------------------------------------- 1 | import React, {Component} from "react"; 2 | import {ThemeProvider} from "../themeContext"; 3 | import {UserProvider} from "../userContext"; 4 | import ContextTypePage from "./ContextTypePage"; 5 | import ConsumerPage from "./ConsumerPage"; 6 | import MultipleContextsPage from "./MultipleContextsPage"; 7 | 8 | class ContextPage extends Component { 9 | constructor(props) { 10 | super(props); 11 | this.state = { 12 | theme: { 13 | themeColor: "red" 14 | }, 15 | user: { 16 | name: "xiaoming" 17 | } 18 | }; 19 | } 20 | changeColor = () => { 21 | const {themeColor} = this.state.theme; 22 | this.setState({ 23 | theme: { 24 | themeColor: themeColor === "red" ? "green" : "red" 25 | } 26 | }); 27 | }; 28 | render() { 29 | const {theme, user} = this.state; 30 | return ( 31 |
32 | {/* 组件跨层级通信 */} 33 | 34 | {/* 如果把这里的MyProvider注释掉,ContextTypePage和ConsumerPage里将取不到theme值,而取默认值pink */} 35 | 36 | 37 | 38 | 39 | {/*多个Context */} 40 | 41 | 42 | 43 | 44 |
45 | ); 46 | } 47 | } 48 | 49 | export default ContextPage; 50 | -------------------------------------------------------------------------------- /16期/lesson2-save/src/pages/ContextTypePage.js: -------------------------------------------------------------------------------- 1 | import React, {Component} from "react"; 2 | import {ThemeContext} from "../themeContext"; 3 | 4 | export default class ContextTypePage extends Component { 5 | static contextType = ThemeContext; 6 | 7 | render() { 8 | console.log("ContextTypePage", this.context); //sy-log 9 | const {themeColor} = this.context; 10 | return ( 11 |
12 |

ContextTypePage

13 |
14 | ); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /16期/lesson2-save/src/pages/MultipleContextsPage.js: -------------------------------------------------------------------------------- 1 | import React, {Component} from "react"; 2 | import {ThemeConsumer} from "../themeContext"; 3 | import {UserConsumer} from "../userContext"; 4 | 5 | export default class MultipleContextsPage extends Component { 6 | render() { 7 | return ( 8 |
9 |

MultipleContextsPage

10 | 11 | {theme => ( 12 | 13 | {user =>
{user.name}
} 14 |
15 | )} 16 |
17 |
18 | ); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /16期/lesson2-save/src/pages/ReduxPage.js: -------------------------------------------------------------------------------- 1 | import React, {Component} from "react"; 2 | import store from "../store"; 3 | 4 | export default class ReduxPage extends Component { 5 | componentDidMount() { 6 | store.subscribe(() => { 7 | this.forceUpdate(); 8 | }); 9 | } 10 | add = () => { 11 | store.dispatch({type: "ADD"}); 12 | }; 13 | minus = () => { 14 | store.dispatch({type: "MINUS"}); 15 | }; 16 | asyAdd = () => { 17 | store.dispatch(dispatch => { 18 | setTimeout(() => { 19 | dispatch({type: "ADD"}); 20 | }, 1000); 21 | }); 22 | }; 23 | render() { 24 | console.log("store", store); //sy-log 25 | return ( 26 |
27 |

ReduxPage

28 |

{store.getState()}

29 | 30 | 31 | 32 |
33 | ); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /16期/lesson2-save/src/store/index.js: -------------------------------------------------------------------------------- 1 | import {createStore, applyMiddleware} from "redux"; 2 | // import {createStore} from "../kRedux"; 3 | import thunk from "redux-thunk"; 4 | import logger from "redux-logger"; 5 | 6 | function countReducer(state = 0, action) { 7 | switch (action.type) { 8 | case "ADD": 9 | return state + 1; 10 | case "MINUS": 11 | return state - 1; 12 | default: 13 | return state; 14 | } 15 | } 16 | 17 | const store = createStore(countReducer, applyMiddleware(logger, thunk)); 18 | 19 | export default store; 20 | -------------------------------------------------------------------------------- /16期/lesson2-save/src/themeContext.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | 3 | export const ThemeContext = React.createContext({themeColor: "pink"}); 4 | export const ThemeProvider = ThemeContext.Provider; 5 | export const ThemeConsumer = ThemeContext.Consumer; 6 | -------------------------------------------------------------------------------- /16期/lesson2-save/src/userContext.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | 3 | export const UserContext = React.createContext(); 4 | export const UserProvider = UserContext.Provider; 5 | export const UserConsumer = UserContext.Consumer; 6 | --------------------------------------------------------------------------------