美文网首页
Fish-Redux中LocalProps的用法

Fish-Redux中LocalProps的用法

作者: 你萧猫大爷 | 来源:发表于2020-06-29 00:12 被阅读0次

    原文链接:http://www.fackyou.org/archives/flutter/2020062943.html

    讲道理这个用法是我从fish-redux的issus的别人的反馈的官方(貌似)提供的解决方案里扒出来的,以我的能力,目前在百度上搜不到!

    LocalProps解决了什么问题?

    比方说:
    你在component里面有个插件,然后这个插件需要一个controller,这个controller为了在effect或者reducer里面能够用到,所以你会存到state中去,按照fish-redux的尿性这个state必须在page中定义然后通过conn来传递到component中。如果你只是在component中定义并初始化,在页面启动的时候会告诉你view里的controller没有被初始化,所以你不得不在page中初始化好了再用。然后你在component的effect或者reducer中调用的时候会发现丫根本没有任何用。因为你需要dispatch到page的action中才能有用,因为component中的controller的state并不会被改变(可能是这样,如果说错了,你当我没说……反正就是用不了)
    这样很尴尬啊!!
    如果你的page下有好几个component,然后每个component都有各自的controller,你还得给他区分,还要把所有controller的操作写在page里面,那还分毛个component啊!

    LocalProps的优点

    component自己保管自己的数据,不需要从page通过conn传递,自己可以修改自己的数据,不需要出发page的action!

    LocalProps的缺点

    LocalProps的state即使修改了也不会触发刷新view,所以如果涉及到view中的数据,就不要存LocalProps了,会让你抓狂!

    LocalProps的用法

    重点来了!
    在state.dart里增加一段:

    class ComponentLocalProps extends LocalProps<ComponentLocalProps> {
      final RefreshController refreshController =
          RefreshController(initialRefresh: false);
    
      ComponentLocalProps(Context<Object> ctx) : super(ctx);
    
      factory ComponentLocalProps.of(ExtraData ctx) {
        return LocalProps.provide((_) => ComponentLocalProps(_)).of(ctx);
      }
    
      @override
      void destructor(Context<Object> ctx) {
        refreshController.dispose();
      }
    }
    

    里面的refreshController是我定义的state数据,也就是说把从page传递过来的state数据和LocalProps的state数据区分开来了,其他的不需要改,就destructor方法里你得把state销毁,这个好像还是很死板的样子!

    然后使用的时候在view.dart里就是这样:

    ComponentLocalProps.of(viewService).refreshController
    

    在effect.dart里是这样:

    ComponentLocalProps.of(ctx).refreshController.loadComplete();
    

    在reducer.dart里是:

    想什么呢?reducer里面有context吗?
    

    看完之后如果你觉得有用,请在原文链接的留言里告诉我,如果没用……你自己研究去!

    相关文章

      网友评论

          本文标题:Fish-Redux中LocalProps的用法

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