老莫的笔记本  
  
查看: 876|回复: 0

Remax 写微信小程序的一个BUG Redux store.subscribe

[复制链接]

662

主题

878

帖子

5139

积分

超级版主

Rank: 8Rank: 8

积分
5139
发表于 2021-2-24 14:48:47 | 显示全部楼层 |阅读模式
  1. import * as React from 'react';
  2. import { View } from 'remax/one';
  3. import store from '../../store/Store';
  4. import { funGetNewsList } from '../../store/ActionCreator';



  5. class Shop extends React.Component{
  6.     constructor(props) {
  7.         super(props);

  8.         // 接收所有数据,然后进行过滤,取出新闻的部分
  9.         let allStore = store.getState();
  10.         this.state = allStore;
  11.         this.change = this.change.bind(this);
  12.         this.lmStoreChange = this.lmStoreChange.bind(this);
  13.         this.m =1;
  14.         // console.log( allStore )
  15.         // 当我写在 change 里时,store.subscribe 会产生多个BUG 但是在这里却正常?
  16.         store.subscribe( this.lmStoreChange ) ;
  17.     }
  18.    

  19.     lmStoreChange(){
  20.         this.m++
  21.         console.log(this.m)
  22.         console.log( '进入了lmStoreChange 方法')

  23.         // 接收所有数据,然后进行过滤,取出新闻的部分
  24.         let allStore = store.getState();
  25.         this.setState( allStore );

  26.     }
  27.     // 改变值
  28.     change(){
  29.         store.dispatch( funGetNewsList() );
  30.         console.log('执行了dispatch方法' )

  31.         // 第一次点击时,先执行了 dispatch 未执行subscribe
  32.         // 第二次点击时, 先执行了subscribe ,再执行 change方法
  33.         
  34.         //  最后一步订阅
  35.         // store.subscribe( this.lmStoreChange ) ;
  36.         // store.subscribe( this.lmStoreChange() );  这样有效果 但是报错
  37.         // store.subscribe(()=>{
  38.         //     console.log('进入了subscribe方法')
  39.         //     this.lmStoreChange()
  40.         // })

  41.     }

  42.     componentWillUnmount = () => {
  43.         this.setState = (state, callback) => {
  44.             return;
  45.         };
  46.     }

  47.     render(){
  48.         return(
  49.             <View >
  50.                 这是商品界面 { this.state.name }
  51.                
  52.                 <View onTap={ this.change } > 点我变 </View>

  53.             </View>
  54.         );
  55.     }

  56. }

  57. export default Shop;
复制代码
得出结论:store.subscribe( this.lmStoreChange ) ;   要写在  constructor(props) {} 里 才可以在 remax 微信架构中正常订阅。【 也或许是我本该如此理解,但是老师教错了 】

灵感来源:  https://blog.csdn.net/weixin_46041654/article/details/103894759  

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表