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

[useLazyQuery] it acts like useQuery after the first query initiation #9263

Closed
rbals0445 opened this issue Jan 4, 2022 · 1 comment
Closed

Comments

@rbals0445
Copy link

rbals0445 commented Jan 4, 2022

Intended outcome:

when after first useLazyQuery execution, it re-execute automatically when changes variables. it acts like a useQuery.
In docs, executing queries in response to events besides component rendering. is it correct execution? if so docs should be updated.

When only we clicked clickB Button it calls lazyFetch. (clickC is just useState hooks) but, when we clicked clickB and clickC in order, it called lazyQuery twice.

Actual outcome:

You can check the result imgs below.

How to reproduce the issue:


const Body = () => {
    const [curpage, setPage] = useState(1);
    const [list, setList] = useState([]);
    const [hasMore, setHasMore] = useState(true);
    const [totalPage, setTotalPage] = useState(0);
    const [text, setText] = useState();

    const val = useContext(MyContext)

    const [lazyFetch, {data, fetchMore}] = useLazyQuery(BID_DATA_LIST_QUERY, {
        variables: {
            page: curpage,
            region: text,
        }, onCompleted: (data) => {
            console.log("completed")
            setList(list => [...list, ...data.getDataList.listData]) // data.getData 이거는 또 안됨

            if (totalPage === 0) {
                setTotalPage(data.getDataList.totalPage);
            }
        },
    })

    return (
        <>
            <button onClick={() => {
                lazyFetch();
                if (curpage === totalPage) {
                    setHasMore(false);
                }
            }}>clickB
            </button>

            <button onClick={() => {
                setText("서울")
            }}>clickC
            </button>
        </>)


}
const BodyContainer = styled.div`
  margin: 0 auto;
  //width: 1728px;
  width: 60vw;
`

export default Body;
  1. click Order (Unexpected)
  • ClickB -> ClickC
    -> ClickC does not call lazyFetch(), but automatically call lazyQuery and called onCompleted callback.
    image
  1. click Order (Expected)
  • ClickC -> ClickB
    -> when clickC there is no call lazyFetch, and when clickB it calls lazyFetch.
    image

Versions

 @apollo/client : "3.5.6"
Mac Latest version
Node version : v16.13.1
npm version : v8.1.2

plz clarify this situation and if it is error, change the docs plz. it wastes my time....

@hwillson
Copy link
Member

hwillson commented Jan 4, 2022

@rbals0445 this is how useLazyQuery was designed. See this comment for more details. We'll get this updated in the docs (which is being tracked in that issue).

@hwillson hwillson closed this as completed Jan 4, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants