{"id":14976440,"url":"https://github.com/k-water/react-blog","last_synced_at":"2025-10-27T20:31:13.470Z","repository":{"id":222783403,"uuid":"119038859","full_name":"k-water/react-blog","owner":"k-water","description":"personal blog design by react","archived":false,"fork":false,"pushed_at":"2020-03-09T09:18:49.000Z","size":2012,"stargazers_count":177,"open_issues_count":2,"forks_count":30,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-02-01T08:03:50.576Z","etag":null,"topics":["axios","babel","react","react-redux","react-router-dom","react-router-v4","reactjs","webpack3"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/k-water.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-01-26T10:29:03.000Z","updated_at":"2024-08-02T00:51:09.000Z","dependencies_parsed_at":"2024-02-16T08:06:32.187Z","dependency_job_id":null,"html_url":"https://github.com/k-water/react-blog","commit_stats":null,"previous_names":["k-water/react-blog"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k-water%2Freact-blog","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k-water%2Freact-blog/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k-water%2Freact-blog/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k-water%2Freact-blog/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/k-water","download_url":"https://codeload.github.com/k-water/react-blog/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238553055,"owners_count":19491367,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["axios","babel","react","react-redux","react-router-dom","react-router-v4","reactjs","webpack3"],"created_at":"2024-09-24T13:53:53.383Z","updated_at":"2025-10-27T20:31:08.054Z","avatar_url":"https://github.com/k-water.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\n\n[![Build Status](https://travis-ci.org/k-water/react-blog.svg?branch=master)](https://travis-ci.org/k-water/react-blog)\n\n### 前言\n在学习react的过程中，深深的被react的函数式编程的模式所吸引，一切皆组件，所有的东西都是JavaScript。React框架其实功能很单一，主要负责渲染的功能，但是社区很活跃，衍生出了很多优秀的库和工具。个人觉得，想要做好一个项目，往往需要其他库和工具的配合，例如`redux`管理数据，`react-router`管理路由等，掌握基本的`webpack配置`和`es6语法`，然后想要提高性能，还有配合react的钩子函数和`immutable.js`，什么时候组件不需要重新渲染，`next.js`服务端渲染等等...\n一直有一个想法就是重构自己的博客，刚好这段时间放假，又刚好学习了react，于是就有了这个项目。\n\n\n\u003e **项目地址**：https://github.com/k-water/react-blog\n 如果觉得不错的话，您可以点右上角 \"Star\" 支持一下 谢谢！ ^_^\n \n \n\u003e **后台管理**：[https://github.com/k-water/blog-admin](https://github.com/k-water/blog-admin)\n\n\u003e **接口**：[https://github.com/k-water/egg-blog](https://github.com/k-water/egg-blog)\n\n![](https://oc1gyfe6q.qnssl.com/18-3-20/38808679.jpg)\n\n### 技术栈\n**前端**\n* react\n* react-redux\n* react-thunk\n* react-router\n* axios\n* eslint\n* maked\n* highlight.js\n* antd\n* es6/7/8\n\n**后台**\n* spring boot\n\n此项目采用前后端分离的实现，后台接口基于RESTful规范设计，只提供数据，前端负责路由跳转，权限限制，渲染数据等。PS：由于我是个前端er，所以这里主要讲的是前端。\n\n### 实现的功能\n* [x] admin增删查改博客\n* [x] 博客标签\n* [x] 博客内容markdown\n* [x] 博客内容页展示目录\n* [x] 返回顶部\n* [x] markdown代码高亮\n* [x] 用户登录注册\n* [x] 用户评论\n* [x] 响应式\n\n### TODO\n* [ ] ~~博客分类~~\n* [ ] ~~点击标签搜索相关博客~~\n* [ ] ~~完善归档~~\n* [ ] ~~优化首页侧边栏~~\n* [ ] ~~收藏页面~~\n* [ ] ~~关于页面~~\n* [ ] ~~部署上线~~\n\n### 2018-02-08更新\n* [x] 博客分类\n* [x] 点击标签搜索相关博客\n* [x] 完善归档\n\n### 2018-02-10更新\n* [x] 优化首页侧边栏\n* [x] 收藏页面\n* [x] 关于页面\n* [x] 部署上线\n\n### 2018-03-17更新\n1、重写后台接口，使用egg，项目地址:[https://github.com/k-water/egg-blog](https://github.com/k-water/egg-blog)\n\n2、完成后台接口对接\n\n### 效果预览\n#### 首页\n![](https://oc1gyfe6q.qnssl.com/18-2-5/20891733.jpg)\n\n#### 内容页\n![](https://oc1gyfe6q.qnssl.com/18-2-5/60787720.jpg)\n\n#### 用户登录\n![](https://oc1gyfe6q.qnssl.com/18-2-5/93902966.jpg)\n\n#### 用户评论\n![](https://oc1gyfe6q.qnssl.com/18-2-5/17032816.jpg)\n\n#### 后台管理\n\n![](https://oc1gyfe6q.qnssl.com/18-2-5/41492220.jpg)\n\n![](https://oc1gyfe6q.qnssl.com/18-2-5/45917169.jpg)\n\n![](https://oc1gyfe6q.qnssl.com/18-2-5/51225877.jpg)\n\n### 个人总结\n#### markdown渲染\n在前端渲染markdown的时候遇到了一点问题，相关的包很多，但是各种包解析的结果都有差异，react周边社区推荐的是` react-markdown`,使用方法也很简单\n\n``` javascript\nimport ReactMarkdown from 'react-markdown'\n\nconst input = '# This is a header\\n\\nAnd this is a paragraph'\nReactDOM.render(\n    \u003cReactMarkdown source={input} /\u003e,\n    document.getElementById('container')\n)\n```\n\n但是发现`react-markdown`对表格的支持不太友好，最后采用了[marked](https://github.com/chjj/marked)，结合`highlight.js`对代码部分实现高亮\n\n``` javascript\nimport marked from 'marked'\nimport hljs from 'highlight.js'\n  componentWillMount() {\n    marked.setOptions({\n      highlight: code =\u003e hljs.highlightAuto(code).value\n    })\n  }\n```\n\n最后解析出来的是一个字符串，还需要将它插入`dom`中，由于安全问题，`React`不提倡将字符串直接插入`dom`中，但React保留了一个API，可以这样做：\n\n``` html\n\u003cdiv className=\"article-detail\" \n  dangerouslySetInnerHTML={{ __html: marked(output)) }} /\u003e\n```\n\n#### React组件化\nreact的组件由dom视图和state组成，state是数据中心，它的状态决定着视图的状态。react只负责UI的渲染，与其他框架监听数据动态改变dom不同，react采用setState来控制视图的更新。setState会自动调用render函数，触发视图的重新渲染，如果仅仅只是state数据的变化而没有调用setState，并不会触发更新。说到组件，就必须了解react组件的`生命周期`，官方的图解如下：\n\n![](https://oc1gyfe6q.qnssl.com/18-2-5/13398466.jpg)\n\n关于这部分的解释网上有很多，可以自行查阅。而我在开发过程用的最多的就是\n* componentWillMount()\n* componentDidMount()\n* shouldComponentUpdate(nextProps, nextState)\n这几个钩子函数了，关于性能优化，可以在`shouldComponentUpdate`上作文章，由于`shouldComponentUpdate`默认返回`true`，简单的方法可以通过比较更新前后的数据结构是否相同来判断组件是否需要重新渲染，这时候就可以采用`immutable.js`了。\n\n#### 组件之间通信\nreact是单向数据流，自上而下的传递数据。解决复杂组件之间通信的方法有很多。一般父子组件通信是最简单的，父组件将一个回调函数传递给子组件，子组件通过`this.props`直接调用该函数与父组件通信。\n\n如果组件之间嵌套很深，可以使用上下文getChildContext来传递信息，这样在不需要将函数一层层往下传，任何一层的子级都可以通过this.context直接访问，react-redux内部实现就是利用此方法。\n\n兄弟组件之间无法直接通信，它们需要利用同一层的上级作为中转站。\n\n#### Redux\nredux不是必须的，如果不是复杂的组件通信，逻辑简单，用context就行。redux并不是react特有的，其他框架也可以使用redux。当初为了学习redux花费了不少时间，一开始并不理解redux中间的操作，看了很多前辈们写的文章才逐渐明白。简单说说redux。\n**redux由三部分组成：store, reducer, action**\n\n![](https://oc1gyfe6q.qnssl.com/18-2-5/1307277.jpg)\n\nstore是一个对象，它主要由三个方法：\n**dispatch**\n用于action的分发，当action传入dispatch会立即执行，有些时候我们不想它立刻触发，可以在`createStore`中使用middleware中间件对dispatch进行改造，例如redux-thunk，不过这是react-radux做的事了。\n**subscribe**\n顾名思义，监听器，监听state的变化，这个函数在store调用dispatch时会注册一个listener监听state变化。\n**getState**\n获取store中的state，当我们用action触发reducer改变了state时，需要拿到新的state里面的数据。getState在两个地方会用到，一是通过dispatch提交action后store需要拿到state里面的数据，二是利用subscribe监听到state发生变化后调用它来获取新的state数据。\n\n说了这么多，store的核心代码其实很短：\n``` javascript\n/**\n * 应用观察者模式\n * @param {Object} state\n * @param {Function} reducer\n */\nfunction createStore(reducer) {\n  let state = null\n  const listeners = []\n  const subscribe = listener =\u003e listeners.push(listener)\n  const getState = () =\u003e state\n  const dispatch = action =\u003e {\n    // 覆盖原对象\n    state = reducer(state, action)\n    listeners.forEach(listener =\u003e listener())\n  }\n  // 初始化state\n  dispatch({})\n  return {\n    getState,\n    dispatch,\n    subscribe\n  }\n}\n```\n\n另一部分，`reducer`是一个纯函数(pure function)，它接收一个state和action作为参数，根据action的type返回一个新的state，如果传入的action type没有匹配到，则返回默认的state，简单实现如下：\n``` javascript\nfunction reducer(state, action) {\n  if (!state) {\n    return {\n      title: {\n        text: \"water make redux\",\n        color: \"red\"\n      },\n      content: {\n        text: \"water make redux\",\n        color: \"green\"\n      }\n    }\n  }\n  switch (action.type) {\n    case \"UPDATE_TITLE_TEXT\":\n      return {\n        ...state,\n        title: {\n          ...state.title,\n          text: action.text\n        }\n      }\n    case \"UPDATE_TITLE_COLOR\":\n      return {\n        ...state,\n        title: {\n          ...state.title,\n          color: action.color\n        }\n      }\n    default:\n      return state\n  }\n}\n```\naction比较简单，它返回一个对象，其中type属性是必须的，同时也可以传入一些其他的数据。\n使用例子如下：\n``` javascript\n// 生成store\nconst store = createStore(reducer)\nlet oldState = store.getState()\n// 监听数据变化重新渲页面\nstore.subscribe(() =\u003e {\n  const newState = store.getState()\n  renderApp(newState, oldState)\n  oldState = newState\n})\n// 首次渲染页面\nrenderApp(store.getState())\nstore.dispatch({\n  type: \"UPDATE_TITLE_TEXT\",\n  text: \"water is fighting\"\n})\nstore.dispatch({\n  type: \"UPDATE_TITLE_COLOR\",\n  color: \"#f00\"\n})\n```\n\n#### React-redux\n`react-redux`则是对redux做了封装，可以在react中直接使用，并且提供了`Provider`和`connect`。\n**Provider**是一个组件，它接受store作为props，然后通过context往下传，这样react中任何组件都可以通过context获取store。\n**connect**是一个函数，也是一个高阶组件(HOC)，通过传入state和dispatch返回一个新的组件，它的写法是如下：\n``` javascript\nconnect(mapStateToProps, mapDispatchToProps, mergeProps, options)(component)\n```\n也可以采用装饰器的写法，这需要babel的支持：\n``` javascript\n@connect(\n\tstate,\n\t{ func }\n)\n```\n具体的不多介绍，迷你实现可以看看这个项目：https://github.com/k-water/make-react-redux\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fk-water%2Freact-blog","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fk-water%2Freact-blog","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fk-water%2Freact-blog/lists"}