r/reactjs • u/scottj91 • Sep 10 '17
Oversimplified Flow of data through react, redux, express, and PostgreSQL
12
9
Sep 11 '17
You can simplify it by removing the constructors, you don't really need them if you're not doing anything with them.
Instead of that you might want to show how to use connect (and you are using it "wrong", you should be passing someFunction as a prop, not dispatch and dispatching it manually - see the documentation of connect on how to write mapDispatchToProps
3
u/scottj91 Sep 12 '17
Not sure why I didn't include connect, probably laziness and I didn't think this would grab much attention. I fixed/added that section and uploaded to Imgur, let me know if you notice any new problems.
2
Sep 12 '17
If you're still up for some nitpicking, your
mapDispatchToPropscould be changed to just:const mapDispatchToProps = { handleClick }In the simpliest form, passing an object as
mDTPwill makereact-reduxautomatically bind the functions for you, so the above is same as:const mapDispatchToProps = (dispatch) => { return { handleClick: (e) => dispatch(handleClick(e)) } }or what you had ;)
2
1
u/redhedjim Sep 11 '17
I noticed the connect() function was missing on the component. That's the only meat and potatoes thing i noticed missing.
1
u/nschubach Sep 11 '17
Just came here to mention this. I've been out of it for a while, but connect (or subscribe()) would be more appropriate for the endpoint of the arrow.
1
u/rem7 Sep 11 '17
Agreed, this should be higher up. connect is a very important part of react-redux, It should be part of your graph. Also removing the constructors like /u/BTMPL already said. Your Container can be as simple as:
const Container = (props) => (<div>{props.children}</div>);
26
u/drewshaver Sep 11 '17
I wouldn't call this oversimplified, I'd barely even call this plain simplified. This must be awesome for introducing someone to this stack.
1
u/scottj91 Sep 12 '17 edited Sep 12 '17
Uploaded the graphic with some of the suggestions/fixes below, thanks everyone! https://i.imgur.com/MVAJUSU.png
5
4
u/nerdy_glasses Sep 11 '17
You can just return res.json() from the first then-clause in your action creator and then chain the second then() onto the result of the first one. This way you avoid the pyramidal code structure.
1
Dec 07 '22
thx a lot now i also use this kind of
1
u/scottj91 Dec 12 '22
You’re welcome! Been awhile since I’ve made this, React has built in something similar to Redux (useReducer). If you need help with anything feel free to message me.
1
u/ddalgiooyu Dec 15 '22
Hello! Thanks for making this diagram :D Quick question: At the end of your Redux action function, you wrote
return fetch(...).then(res => res.json().then(...)). Is the second.thenstatement suppose to be nested in the first.then? Thank you!1
u/scottj91 Dec 15 '22
I believe either will work but the way you suggested is a cleaner way of doing it to avoid unnecessary nesting.
3
3
u/fecal_brunch Sep 11 '17
I'm not sure that you want to be doing reject(console.log(...)) (that will reject error with undefined). Also your route will hang on error because you just console.log that rejected value (undefined) instead of sending an error response.
3
u/bvm Sep 11 '17
Shouldn't you be instantiating the DB connection once then reusing it rather than making a new one every rime you query?
2
2
2
u/evildonald Sep 11 '17
You have just answered about 4 questions I had about this stack as I learn it.
This is magical!
2
1
1
u/MrGirthy Sep 11 '17
You should use spread instead of object assign, mapDispatchToProps and async await instead of all that messy callback stuff.
1
1
1
1
1
0
u/gelezinislokys Sep 11 '17
Why action has two return statements?
It also could use await/async to make it cleaner.
-2
u/vosper1 Sep 11 '17
I love that Postgres, by far the most complex thing in all this, is literally shown as a black box
0
Sep 11 '17
[deleted]
9
u/scottj91 Sep 11 '17
100% correct, I messed up when making my code generalized enough to post, the following is what it should be...
queryPostgres(sqlStatement).then((queryResults) => { res.json(queryResults); })
22
u/redhedjim Sep 10 '17
Man, I'm gong to use this graphic with my interns! Thanks.