-
I have the following API slice: const mainApi = createApi({
reducerPath: 'mainApi',
baseQuery: fetchBaseQuery({
baseUrl: 'http://localhost:3001/api/v1',
}),
tagTypes: ['$USER'],
endpoints: (builder) => ({
login: builder.mutation({
query: ({ email, password }) => ({
url: '/auth/login',
method: 'POST',
body: {
email,
password,
},
credentials: 'include',
}),
transformResponse: (res) => res.data,
// invalidatesTags: ['$USER'],
async onQueryStarted(payload, { dispatch, queryFulfilled }) {
console.log('LOGIN QUERY STARTED');
try {
const { data: $USER } = await queryFulfilled;
dispatch(
mainApi.util.updateQueryData('getLoggedInUser', null, (draft) => {
console.log('METHOD DISPATCH RAN!!');
Object.assign(draft, $USER);
})
);
} catch (err) {}
},
}),
getLoggedInUser: builder.query({
query: () => ({
url: '/auth/logged-in-user',
credentials: 'include',
}),
transformResponse: (res) => res.data,
providesTags: ['$USER'],
}),
}),
}); and I have the following component which uses this API slice: export default function Login() {
const navigate = useNavigate()
const [login, { isError, isSuccess, isLoading, error, data }] = useLoginMutation()
useEffect(() => {
switch (true) {
case isError:
console.log("ERROR!")
break
case isSuccess:
console.log('SUCCESSFULY LOGGED IN!, userdata:', data)
navigate('/', { replace: true })
}
}, [isError, isSuccess])
const formSubmit = (e) => {
e.preventDefault()
const formData = new FormData(e.target)
const email = formData.get('email')
const password = formData.get('password')
login({ email, password })
}
return (
<form onSubmit={formSubmit}>
<TextField name="email" type="email" required />
<TextField name="password" type="password" required />
<button type="subbmit">LOGIN</button>
</form>
)
} when I input the email and the password on the front-end and click LOGIN I can successfully log in. but it prints the following in the console:
but it does not print METHOD DISPATCH RAN!! which is causing other things in my app to fail, can you help me? |
Beta Was this translation helpful? Give feedback.
Answered by
phryneas
Aug 5, 2022
Replies: 1 comment 3 replies
-
Assuming you are calling that query without an arugment, it's
and it will only update that data if it already was in cache. |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
samislam
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Assuming you are calling that query without an arugment, it's
undefined
notnull
.and it will only update that data if it already was in cache.