周大胖子 发表于 2021-2-24 14:48:47

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

import * as React from 'react';
import { View } from 'remax/one';
import store from '../../store/Store';
import { funGetNewsList } from '../../store/ActionCreator';



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

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

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

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

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

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

    }

    componentWillUnmount = () => {
      this.setState = (state, callback) => {
            return;
      };
    }

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

            </View>
      );
    }

}

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

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

页: [1]
查看完整版本: Remax 写微信小程序的一个BUG Redux store.subscribe