-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
How to modify data that getting from SWR #402
Comments
you can make the const {data} = useSWR(key, async () => { const {data} = await axios.get(API); return data; }) @xyyolab do you mind providing more information about the use case? when user input sth, changes will lead to a new get request and the response data, which will change the value again and it will go into the new cycle? |
Thanks for your response! I figure out this possible way to do my job. I'm new to SWR which is great! Do you have any advice on this situation? function productDetail () {
const [value, setValue] = useState('')
const [fetched, setFetched] = useState(false)
const fetcher = (url) => fetch(url).then((r) => r.json())
const { data } = useSWR(GET_API, fetcher)
const handleClick = async () => {
await axios.post(POST_API, {value})
}
useEffect(() => {
if (data) {
setValue(data.value)
setFetched(true)
}
}, [data])
if (!fetched) return <div>loading</div>
return (
<>
<input value={value} onChange={(e) => setValue(e.currentTarget.value)} />
<button onClick={handleClick}>submit</button>
</>
)
} |
thanks I think I understand your approach here 😄 looks great. not sure if the const {data, isValidating} = useSWR
if (isValidating) return <div>loading</div> |
You need to use the mutate function, either the one returned by useSWR with the key already applied if the one exported by the library without the key already applied. import { mutate } from “swr”
mutate(key, oldData => newData, false) Or const { mutate } = useSWR(key, fetcher)
// ...
mutate(oldData => newData, false) |
@sergiodxa I think I need to use
|
Something like this should do the work: const fetcher = (url) => fetch(url).then((r) => r.json());
function ProductDetail() {
const { data, mutate } = useSWR(GET_API, fetcher, {
revalidateOnFocus: false, // disable onTabFocus revalidation
});
const handleClick = async () => {
await axios.post(POST_API, { value: data });
mutate(); // revalidate data
};
function handleChange(event) {
// mutate the data without revalidating it
mutate(event.currentTarget.value, false);
}
if (!data) return <div>loading</div>;
return (
<>
<input value={data} onChange={handleChange} />
<button onClick={handleClick}>submit</button>
</>
);
} |
Thanks man, this is what i was looking for |
How to migrate this use case to
SWR
?Thanks in advance!
The text was updated successfully, but these errors were encountered: