Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Taro.createIntersectionObserver(this).relativeToViewport({bottom: 0}).observe 监听某组件, 无论后面dispatch,再读取state 永远是默认值 #4752

Closed
languis opened this issue Nov 4, 2019 · 1 comment
Assignees

Comments

@languis
Copy link

languis commented Nov 4, 2019

问题描述
.footer 到视窗可视位置时,observe回调读取hooks 的state,state值每次都是默认defaultState,而不是最新的state

image

import Taro, { Component, useEffect} from '@tarojs/taro'
import { View } from '@tarojs/components'
import { getCourses } from '@@/api/common'
import { LIST_REQUEST_BEGIN, LIST_REQUEST_FAILURE, LIST_CLEAR, LIST_RECEIVE } from '@@/constants/list'
import './LsCourseList.scss'

/// 获取课程
function getCourses(dataFn) {
  const [state, dispatch] = useReducer(makeReducer(dataFn), defaultState);
  const fetch = makeFetch('http://127.0.0.1:3721/v1/course');

  const reset = () => {
    fetch(dispatch, state);
  }

  return [state, dispatch, reset, fetch];
}

export default function () {
  const [coursesState, dispatchCourses, resetCourses, fetch] = getCourses();

  useEffect(() => {
    Taro.createIntersectionObserver(this).relativeToViewport({bottom: 0}).observe('.footer', res => {
      setTimeout(() => {
        if (res.intersectionRatio > 0 && coursesState.status == LIST_RECEIVE) {
          /// 此处state 永远是默认defaultState
          console.log(coursesState);
          /// 此次从远程获取数据,并通过dispatch 修改了 state
          fetch(dispatchCourses, coursesState);
        }
      }, 3000);
     });
   }, []);

  /// 这里state 返回的的数据是改变后的state
  console.log(coursesState);

   debugger;
  const courses = coursesState ? coursesState.data : [];
  
  return <View className='LsCourseList'>
    { 
      courses.map(() => <View className='item'>item</View>)
    }

    <View className='footer'>
      footer
      { coursesState.msg }
    </View>
  </View>
}

期望行为
.footer 到视窗可视位置时,observe回调读取hooks 的state,state值每次都是默认defaultState,而不是最新的state

报错信息

系统信息

Taro v1.2 及以上版本已添加 taro info 命令,方便大家查看系统及依赖信息,运行该命令后将结果贴下面即可。

  • 操作系统: mac 10.15
  • Taro 版本 👽 Taro v1.3.19
  • Node.js 版本 v11.9.0
  • 报错平台 weapp
@taro-bot
Copy link

taro-bot bot commented Nov 4, 2019

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants