Comments (5)
ok..clearly I was making this all too complicated.
The simple way to achieve it is:
const ConnectedForm = apiConnector({
updateThing: (body, then) => ({
updateThingResponse: {
method: 'patch',
url: `/thing/${body.id}/`,
body,
then,
},
}),
})(Form);
Then in, for example, your onSubmit handler:
onSubmit={values =>
updateThing(values, () => {
// stuff here
})
}
I still like the idea of it returning a promise from refetchDataFromMappings
which might be achieved by returning mappings
from that, then access it as updateThing(values).then({updateThingResponse})
... but this is not tested.
from react-refetch.
I'm stilling running into problems. It will create an infinite loop because it triggers a re-render. To me seems like a bug. If the this.props.postLike(someSubject)
callback could return a promise, I think that could make this work.
It looks like it calls refetchDataFromMappings
here:
react-refetch/src/components/connect.js
Line 242 in 5519f20
But... it does not look like there would be an easy for it to return a promise.
Thoughts? or, suggestions on what to do? @ryanbrainard
from react-refetch.
Aha... using the PromiseStateContainer, I could use shouldComponentUpdate.
shouldComponentUpdate(nextProps) {
const { ps } = this.props;
// ps changed, update
if (!nextProps.ps || ps !== nextProps.ps) return true;
if (ps) {
if (
ps.fulfilled !== nextProps.ps.fulfilled ||
ps.pending !== nextProps.ps.pending ||
ps.rejected !== nextProps.ps.rejected
) {
// Status changed, update
return true;
}
// Status did not change, don't update
return false;
}
return true;
}
But... shouldComponentUpdate
is not guaranteed to work like this in the future, since "In the future React may treat shouldComponentUpdate() as a hint rather than a strict directive, and returning false may still result in a re-rendering of the component." Bummer.
I did find a workaround but it still feels hacky since it uses the andThen
hack I mentioned in my first post.
It seems like the way I can achieve what I want is to actually not use react-refetch
for the case when I want to dispatch an action / etc.
from react-refetch.
@fc nice! And is it possible to access the response header in the callback?
For example:
onSubmit={values =>
updateThing(values, (response) => {
if (response.status === 500) {
// do something to the 'thing'
// perhaps also try again...
}
})
}
from react-refetch.
({
myPut: (data, thenCallback, catchCallback) => ({
myPutResponse: {
url: 'some-url',
method: 'PUT',
body: JSON.stringify(data),
andThen: (value, meta) => {
thenCallback(value, meta);
return {};
},
andCatch: (reason, meta) => {
catchCallback(reason, meta);
return {};
}
}
})
}),
And then in the component:
const onSubmitPutSuccess = (value, meta) => {
const { response: { status } } = meta;
if ( status === 200) {
// all good, do something
}
}
const onSubmitPutError = (reason, meta) => {
const { response: { status } } = meta;
if ( status === 500) {
// do something with the error
}
}
myPut(
payload,
(value, meta) => onSubmitPutSuccess(value, meta), // thenCallback
(reason, meta) => onSubmitPutError(reason, meta), // catchCallback
);
from react-refetch.
Related Issues (20)
- Identity requests meta does not have component HOT 1
- Accessing the response body in case of reject HOT 2
- impossible to update my props into react-refetch HOT 1
- Update Lifecycle Hooks for Future Async Rendering HOT 3
- skip/disable/remove fetch config conditionally HOT 4
- Doesn't support SSR HOT 5
- [idea] useRefetch HOT 5
- Refresh POST method request doesn' work HOT 1
- [Typescript] Type of "body" attribute must be string? and not object? on the Mapping interface
- Can you retry a failed request from within render? HOT 1
- Typings problem with PromiseStateMapping/FunctionMapping HOT 1
- Pass this.state.data to then(), catch(), andThen() and andCatch() functions for more powerful side effects HOT 2
- Override default behavior on failure of fetch with refreshInterval HOT 1
- Typescript types seem not to work properly HOT 9
- react 16.9, Warning: componentWillMount has been renamed, and is not recommended for use HOT 2
- Fetch rejected but reason empty HOT 2
- Example in README cannot be compiled in TypeScript HOT 1
- Is it possible to use a callback of a request? HOT 4
- Support for react@17 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-refetch.