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

希望InfiniteScroll能增加loading属性 #4735

Open
chenliangngng opened this issue Jan 25, 2022 · 12 comments
Open

希望InfiniteScroll能增加loading属性 #4735

chenliangngng opened this issue Jan 25, 2022 · 12 comments

Comments

@chenliangngng
Copy link
Contributor

Version of antd-mobile

v5.0.0-rc.21

What is this feature about?

目前loading状态是通过loadMore函数确定的,希望直接传入loading来进行更高优先级控制

@awmleer
Copy link
Member

awmleer commented Jan 27, 2022

可以具体描述一下你的使用场景么?比较好奇在什么情况下需要用这个额外的 loading 属性来手动控制

我觉得加不加 loading 属性是其次的,重要是找到当前 API 使用起来不那么方便的场景,然后再看看这个场景存在的问题通过什么方式来解决才是最合理的,未必是加个 loading 属性这么简单

@awmleer awmleer added the pending Need more information to continue label Jan 27, 2022
@chenliangngng
Copy link
Contributor Author

chenliangngng commented Jan 27, 2022

调用下一页的动作,不是直接调用promise接口,而是写在useEffect里面。比如点击下一步,修改了currentPage,触发setSearchParams,然后由于searchParams变化,触发接口函数

const [searchParams, setSearchParams] = useState()
useEffect((
)=>{
// fetch
},[searchParams])

@awmleer

@awmleer
Copy link
Member

awmleer commented Jan 27, 2022

@chenliangngng OK 了解了

不过我还有个小疑惑哈,假如说我们给 InfiniteScroll 增加了 loading 属性,那你会怎么使用 loading 属性来解决这个场景的问题呢?可以再写一下 demo 么

@chenliangngng
Copy link
Contributor Author

chenliangngng commented Feb 8, 2022

@chenliangngng OK 了解了

不过我还有个小疑惑哈,假如说我们给 InfiniteScroll 增加了 loading 属性,那你会怎么使用 loading 属性来解决这个场景的问题呢?可以再写一下 demo 么

const [searchParams, setSearchParams] = useState()
const [loading, setLoading] = useState()
useEffect(()=>{
  try{
    setLoading(true)
    // fetch
  } finally {
    setLoading(false)
  }
},[searchParams])

@awmleer
Copy link
Member

awmleer commented Feb 8, 2022

get,其实现在的问题是 InfiniteScroll 对于手动数据加载的情况不太适用,我再想一下怎么处理比较好

@awmleer
Copy link
Member

awmleer commented Feb 8, 2022

这种场景之前的确也有其他社区同学提到过

@xiaoyao96
Copy link
Collaborator

xiaoyao96 commented Feb 11, 2022

有两个方案可以参考:
1、支持ref,自定义发起加载更多,并自带loading:

type InfiniteScrollRef = {
    loadMore: (customLoadMoreFn?: () => Promise<unknow>) => void;
}

function C(){
   const ref = useRef<InfiniteScrollRef>(null);
   async function loadMore(){
      // fetch1
   }
   return <>
      <Button onClick={() => {
            //调用InfiniteScroll自身loadMore
            ref.current.loadMore();
            // 需要自定义加载则入参
            ref.current.loadMore(async function(){
                 // fetch2
            });
       }}>点击加载更多</Button>
      <List>...</List>
      <InfiniteScroll loadMore={loadMore} />
   </>
}

2、给InfiniteScroll组件增加优先级更高的loading属性,不传时则组件内部控制,传入时则完全受控。

@awmleer awmleer removed the pending Need more information to continue label Feb 17, 2022
@awmleer awmleer self-assigned this Feb 17, 2022
@awmleer
Copy link
Member

awmleer commented Jun 8, 2022

做成一个属性来切换受控非受控感觉有点太恶心了,而且用户用起来设想了一下感觉也不省事

或许可以这样通过 ref 来手动控制:

const infiniteScrollRef = useRef()

async function doRequest() {
  infiniteScrollRef.current.startManualTask()
  try {
    await api.myRequest(pageCount)
    infiniteScrollRef.current.finishManualTask()
  } catch (e) {
    infiniteScrollRef.current.failManualTask()
  }
}

useEffect(() => {
  doRequest()
}, [pageCount])

<InfiniteScroll
  ref={infiniteScrollRef}
  loadMore={(isRetry) => {
    if (isRetry) {
      doRequest()
    } else {
      setPageCount(v => v + 1)
    }
  }}
/>

上面的 demo 看起来有点复杂,是因为同时考虑到了请求异常的处理

@zqran
Copy link
Collaborator

zqran commented Sep 19, 2022

@awmleer startManualTask() 这个方法,需要干什么? 要区分出手动加载数据吗?

@awmleer
Copy link
Member

awmleer commented Sep 20, 2022

@zqran 当时我设想的是,有两套状态,分别是手动模式和自动模式对应的 hasMore failed 之类的状态,然后手动模式的优先级更高,不过感觉可能也不好实现?

感觉还是得再从具体的 case 出发,才能知道哪种 API 设计是最合理的

@zqran
Copy link
Collaborator

zqran commented Sep 20, 2022

@awmleer 可以再参考下 #5653@mnm1001 的回复

@awmleer
Copy link
Member

awmleer commented Sep 20, 2022

OK~回复了

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

No branches or pull requests

4 participants