美文网首页
移动端zapp优化

移动端zapp优化

作者: 雷霆崖带头大哥 | 来源:发表于2017-09-14 18:51 被阅读0次

WyComponet


现在的移动端封装的widget-util在react和redux联系建立起来比较繁琐,不少同事吐槽这个库不好用.我也用得很不爽所以写了个WyComponet来简化redux的使用

原来的zapp库的使用

下面是“云打印”上使用widget-util的一个模块

import { registerComponent, WidgetComponent, getComponentState, reduceComponentState } from 'widget-utils';
 class PrintDes extends WidgetComponent {
    static statePath = 'PrintDes';
    static getInstanceInitState() {
        return {
            printSettingDetail: {}
        }
    }
    static mapStateToProps(state, ownProps) {
        const instanceState  = getComponentState(state, PrintDes)
        return { instanceState }
    }
    static componentReducer(state, action) {
        let instanceState = getComponentState(state, PrintDes);
        let newState = {};
        switch (action.type) {
            case 'PRINT_SETTING_DETAIL':
                newState = {
                    ...instanceState,
                    printSettingDetail: action.data
                };
                window.PrintDes = action.data;
                return reduceComponentState(state, newState);
        }
        return state;
    }
    componentWillMount(){
         this.getPrintSetting()
    }
    getPrintSetting() {
        //请求分类数据
        callApi("/evh/siyinprint/", "/getPrintSetting", {  
        }).then((json) => {
            if (json.response) {
                this.dispatch({type: 'PRINT_SETTING_DETAIL', data: json.response})
            }
        }
    }
    goBack(){
        this.props.history.push("/home")
    }
    render() {
        return (<div>
            </div>)       
        )
     }
 }
 export default registerComponent(PrintDes);
  1. statePath 声明component的命名空间
  2. getInstanceIntState 设置初始的state值
  3. mapStateToProps 建立state与component的映射关系
  4. componentReducer 设置component的reducer的值
  5. registerComponent(PrintDes) 注册component

业务还没开始,就写了一大堆,严重影响开发心情.

所以重点来了,各位同学要认真听咯

新的zapp库

下面是“服务联盟web”的一个模块

import React, {PropTypes} from 'react';
import ReactDOM from 'react-dom';
import {WyComponent, registerComponent} from "widget-utils";
import "./index.less"

const {detail} = createActions("DETAIL");
class Detail extends WyComponent{
    static statePath = "Detail";
    static getInitState = ()=>{
        return {
            detail: {}
        }
    };
    static componentReducer = {
        [detail]:"detail"
    };
    componentWillMount(){
        var id = this.props.match.params.id;
         Request("/evh/yellowPage/getServiceAllianceEnterpriseDetail", {
        
            }).then((data) => {
                this.dispatch(detail(data))   
            })
    }
    componentDidMount(){
    }
    render(){
        let detail = this.props.detail
        return (<div className={prefix}>
        
        </div>)
    }
}

export default registerComponent(Detail)
  1. statePath 设置component的命名空间
  2. componentReducer 建立state与component的映射关系
  3. registerComponent 注册

是不是简化了很多?

Api

 api说明

内置方法

方法 说明 参数
dispatch 发送action action
getState 拿到所有的state

静态属性

方法 说明 参数 key value
getInitState 设置初始的state的值
componentReducer 建立redux和componet的映射关系 一个消息字符串 componet映射的值

说明:componentReducer = { [detail]:"detail" } 就是当dispatch一个消息将更新组件里面的this.props.detail的值 也可以是 componentReducer = { [detail]:(state, action)=>({...state,detail: action.payload}) }

相关文章

  • 移动端zapp优化

    WyComponet 现在的移动端封装的widget-util在react和redux联系建立起来比较繁琐,不少同...

  • Rhyke.js 使用例子

    移动端桌面端无法同时开启。移动端长按方案有问题。判断错误算法可优化。

  • ios - 收藏集 - 掘金

    天弘基金移动 App 客户端架构优化之路 - iOS - 掘金天弘基金移动App客户端架构优化之路 随着移动互联网...

  • 移动端优化

    1、解决点击 300ms 延迟 问题原因:点击的 300ms 延迟是由双击缩放机制所导致的,由于用户可以进行双击缩...

  • 移动端优化

    1. 代码优化##### css和js文件需要uglify 使用grunt等构建化工具 2. 图片优化##### ...

  • 前端性能优化常用总结

    正文 前端优化层出不穷,移动端大行其道的现在,我们可以说优化好移动端,PC端也将会更好。所以,我们可以综合以下图片...

  • 搜索引擎优化复盘9,移动端SEO

    第九章 移动端SEO 1.1 移动端SEO 移动端SEO指的是优化用户在智能手机和平板设备上访问网站的体验,移动...

  • SEO之移动搜索优化

    移动搜索优化与PC端优化差别不大,除了PC端相同的优化除外,在移动搜索优化时我们主要考虑的是代码层面的优化,要让用...

  • H5移动端的性能优化

    H5 移动端的性能优化V1.0 一、渲染优化 二、css优化 三、加载优化 四、脚本执行优化

  • Web移动端优化

    1、使用 touchstart 代替 click 由于移动端屏幕的设计, touchstart 事件和 click...

网友评论

      本文标题:移动端zapp优化

      本文链接:https://www.haomeiwen.com/subject/zjndsxtx.html