score:12

Accepted answer

if you use a query, you would use local component state to set the query parameter

const [mystate, setstate] = usestate(skiptoken) // initialize with skiptoken to skip at first
const result = usemyquery(mystate)

and in your click handler you then set the state

const changepage = () => {
  setstate(5)
}

in your case though, you have a form submit and that sounds you want to use a "mutation", not a query. the point of mutations is that you are not really interested in the result long-term, but rather want to trigger a change on the server by sending data.

const [trigger, result] = usemymutation()

that would be called like

const handlesubmit = () => {
  trigger(somevalue)
}

score:6

there is another option to pass boolean as the second argument of the query.

for example:

i use this for login:

const [email, setemail] = usestate<string>('')
const [password, setpassword] = usestate<string>('')
const [logincredentials, setlogincredentials] = usestate<iloginrequest>({ email: '', password: '' })

const { data, error, iserror, issuccess, isloading } = useloginquery(logincredentials, {
    skip: logincredentials.email === '' && logincredentials.password === '',
})

note: the skip parameter is false when the component load. when user fills email and password i'm setting the email and password state. and when press the login button i set the logincredentials state and the rtk query is triggered.


Related Query

More Query from same tag