Comments (9)
Was also looking for a solution to this, and ended up writing my own (of course :-P), might be interesting for others ending up here, at least for inspiration:
https://github.com/tomat/react-reflorp
Basically I really like the way Refetch does things, but I still wanted less boilerplate, and to keep things in the Redux store. Ended up with something of an ORM, where it keeps track of which component needs which entity with which id, and stores them separately in Redux, but still allowing use of regular Redux and Refetch within the same decorators. Not in any way production ready, though :-)
Feel free to open an issue over there if you have a question...
from react-refetch.
I think it should just work, unless Refetch and Redux connect
imports clash. I imagine passing your component to one connect
and pipe it to the next one and you should have all required props available.
I'll keep an eye on this discussion though.
from react-refetch.
You probably also want to make sure that the props actually changed or at least that the PS changed from pending to fulfilled; otherwise, you'll end up calling the action multiple times.
Without thinking about it too much, this pattern feels a bit too imperative to me. If this really is a pattern that people would like to use, I'd like to see something more declarative and/or reactive. Furthermore, I'd really like to see use cases/stories so we are designing this to solve real world problems instead of starting with the technology.
Thanks for kicking off this discussion.
from react-refetch.
@ryanbrainard connecting both RR and Redux to the same component doesn't worked well on my tests. Handling updates inside componentWillReceiveProps
was a mess.
I propose a different approach:
connect((props, state, dispatch) => ({
projectsFetch: '/api/projects.json',
someStateFromRedux: state.someStateFromRedux,
onSomething: () => dispatch(someReduxAction())
}))(Projects)
Maybe you can enable mapping state and dispatch as additional properties. As store will be available through this.context
maybe you can check for it and enable state/dispatch mapping if it's available.
from react-refetch.
+1
from react-refetch.
As I can't make RR work with Redux for now I've created a side project based on RR ideas for cases that you really need Redux.
Here is a sample usage: https://github.com/hnordt/reax/blob/f148edeb1fa870c12f0b7a26c603101fa4990cb2/src/projects/containers/Projects.js
Repo: https://github.com/hnordt/reax-api
While it's already working and I'm using in production, it doesn't cover all RR features (I don't even know if it will, as it's not the point).
The cool part is that it's already very flexible (if you really need Redux).
You just need to dispatch a callAPI
action, the first param is id
, then id
will be available to be read from state.api.<id>
. For example:
// data will contain endpoint data in case of both success or failure
// isLoading will be set to true if request is not complete
// error will be set to true in case of failure
const mapStateToProps = state => {
const {
data: projects, // just renaming data to projects because it'll be passed to a component
isLoading = true, // isLoading will default to true
error
} = state.api.projects || {}; // state.api.projects will be undefined before the request, so we need a empty object for destructing
return {
projects,
isLoading,
error
};
};
const mapDispatchToProps = dispatch => ({
// callAPI(id, method, url, params, dispatchRequest = true)
// if you change dispatchRequest to false, isLoading won`t be changed
loadProjects: () => dispatch(callAPI('projects', 'GET', '/api/projects'))
});
from react-refetch.
+1
from react-refetch.
@hnordt where did your project go!? I need it :)
Take a look at https://github.com/facebook/flux/blob/master/examples/flux-chat/js/actions/ChatServerActionCreators.js and https://facebook.github.io/flux/docs/chat.html
You can workaround by dispatching an action UPDATE_ALL
to put the RR .value
into the redux store
Also http://redux.js.org/docs/advanced/ExampleRedditAPI.html presents a solution. No need of react-refetch then, if you need your data in the store.
from react-refetch.
Closing this out as it seems like the discussion has died off, but feel free to re-open if needed.
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
- Posting data and having it return as a promise HOT 5
- 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.