解析SkyBlog中store中存储的state

说到state,我们肯定从Reducer入手,先来看下AppReducer

const AppReducer = combineReducers({
    navigation : NavigationReducer,
    articles : ArticlesReducer,
    content : ContentReducer,
    upload : UploadReducer,
    login : LoginReducer,
    users : UsersReducer,
    classify : ClassifyReducer,
    about : AboutReducer,
    msg : MsgBoardReducer,
    github : GithubReducer,
    sys_log : SysLogReducer,
    form : formReducer,
    router: routerReducer,
    notifications: notificationsReducer()
});

除了倒数3个分别是redux-formreact-router-reduxreapop提供的Reducer,其他均是项目本身的Reducer,存储着页面中所有的数据。

Navigation State

存储了项目中使用的所有Modal的显示状态,当然默认全是false啦。

const initialState = {
    modal : {
        loginModal_show : false,
        registerModal_show : false,
        userInfoModal_show : false,
        userManagementModal_show : false,
        sysLogModal_show : false
    }
};

Login State

登录相关的state,是非常重要的state,存储了

  1. 登录是否成功
  2. 登录用户信息
  3. 还有是否开启管理模式的信息

其中这个management便是SkyBlog实现阅读与管理合一的核心。

const initialUser = {
    id: 0,
    username : "",
    nickname : "",
    admin : false
};

const initialState = {
    ok : null,
    message : null,
    management : false,
    user : initialUser
};

Articles State - 文章列表

存储了文章和分类文章的分页信息

其中loading属性在之后不少地方也有出现,这个属性是实现加载中的属性

const initialClassify = {
    loading: true,
    id : 0,
    name : "",
    page : {
        list : [],
        page_num : 1,
        pages : 0,
        total : 0,
    }
};

const initialState = {
    page : {
        list : [],
        page_num : 1,
        pages : 0,
        total : 0,
    },
    loading: true,
    classify : initialClassify
};

Classify State - 文章分类

存储分类信息,/articles的右侧导航栏数据便出自于此

show则是编辑器用于判断是否显示新增分类的属性

const initialState = {
    list : [],
    show : false
};

Content State - 文章内容

整个项目最重的一个state,也是博客的核心——文章页面的state,存储了

  1. 文章信息
  2. 文章下的评论列表
  3. 选择回复的评论
export const initialArticle = {
    id : 0,
    title : "文章标题",
    sub_title : "文章副标题",

    classify : {
        id : 1,
        name : "未分类"
    },
    classify_id : 1,

    content : {
        text: "#### 文章内容\n`Markdown编辑器`",
        selection: null
    }
};

const initialComments = {
    list : [],
    page_num : 1,
    pages : 0,
    total : 0
};

export const initialPreviousComment = {
    id : 0,
    author : {
        id : 0,
        nickname : ""
    },
    content : ""
};

const initialState = {
    comments : initialComments,
    comments_loading : true,

    article : initialArticle,
    article_loading: true,

    previous_comment : initialPreviousComment
};

Upload State - 上传

存储了上传文件Modal的显示、服务器访问的信息

const initialState = {
    uploadModal_show : false,
    response : {
        ok : null,
        message : null,
        url : ""
    }
};

About State - 关于

存储了关于的信息

const initialState = {
    content : {
        text: "",
        selection: null
    },
    loading : true
};

MsgBoard State - 留言板

存储了留言列表、选择回复的留言的信息

const initialState = {
    page : {
        list : [],
        page_num : 1,
        pages : 0,
        total : 0
    },
    loading : true,
    previous_comment : initialPreviousComment
};

Users State - 用户

存储了用户列表的信息,只在UserManagementModal显示

const initialState = {
    list : [],
    page_num : 1,
    pages : 0,
    total : 0,
};

Github State - SkyBlog动态

存储了SkyBlog的项目Commits信息

const initialState = {
    commits : [],
    loading : true
};

SysLog State - 系统日志

存储了系统日志列表的信息,只在SysLogModal显示

const initialState = {
    list : [],
    page_num : 1,
    pages : 0,
    total : 0,
};

以上便是项目中存储的State,然后还有三个第三方State:

  1. react-router-redux - 存储路由信息
  2. redux-form - 存储表单信息
  3. notifications - 通知系统reapop存储的信息

以上便是项目中ViewModel中存储的信息啦,不得不说Redux可以说是SkyBlog的MVP,没有状态管理工具估计要写半天上下文的代码。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 学习必备要点: 首先弄明白,Redux在使用React开发应用时,起到什么作用——状态集中管理 弄清楚Redux是...
    贺贺v5阅读 12,906评论 10 58
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,837评论 25 709
  • Redux对于React程序是可有可无的吗?当你认识到Redux在编程时给你那种可以掌控一切状态能力的时候,你会觉...
    smartphp阅读 4,804评论 0 5
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,237评论 19 139
  • 山娃_96e5阅读 1,738评论 0 1

友情链接更多精彩内容