Fork me on GitHub
#reagent
<
2018-03-26
>
exit215:03:09

Is there a general pattern for fetching from an API on a component load?

exit215:03:24

similar to React.. componentWillMount, etc.

pesterhazy15:03:37

you can do it on componentDidMount if you want, or more idiomatically in the outer fn of a Form-2 component

exit215:03:08

@pesterhazy I’m not familiar with a Form-2 component, do you have any resources you can share?

exit215:03:19

that works 😄

pesterhazy16:03:06

note that this won't update if the props change, so if your requests depend on the props you man need to fire a request on componentWillReceiveProps/componentDidUpdate

pesterhazy16:03:23

in which case you'll need to use a React-style Form-3 component

exit216:03:43

I’m fetching based on a query param in the route, which I end up storing in app-state

exit216:03:14

if it exists, I’ll be fetching - otherwise everything as normal

pesterhazy16:03:16

then you should consider doing the fetch as an effect of the new location

pesterhazy16:03:27

components are often not the best place for network requests

pesterhazy16:03:52

usually what works best for me is to react to user actions with network requests

pesterhazy16:03:23

one user action of course may be page load; another may be changing the url (you can listen to those changes)

pesterhazy16:03:40

that's also the approach that the re-frame philosophy encourages

pesterhazy16:03:26

for some components, though, component lifecycle methods may be a convenient shortcut

pesterhazy16:03:54

the appropriate place for network requests is a pretty fundamental question in designing your SPA

exit216:03:04

so the route would handle the fetching rather than the component itself

justinlee16:03:34

for the record though, the load on component cycle will work fine. and can you do the presentational/container pattern if you’d like. there are advantages to not doing it that way but it will work fine. particularly if you put the url in the state object because then you get a re-render if there’s a change (and don’t have to deal with form-3)

exit216:03:58

yeah I’m just looking for the more reagent style I guess

pesterhazy16:03:17

correct! think of the route handler as "controller", and component code more as "view" parts of MVC

justinlee16:03:19

there is no “reagent” style, though there is a “re-frame” style

pesterhazy16:03:37

I agree, reagent is neutral to how you structure your app

exit216:03:52

yeah I have subscribed to re-frame as of yet, but it may be in my future

pesterhazy16:03:25

also agree with @lee.justin.m that lifecycle methods are a good solution (especially at the beginning of the project)

justinlee16:03:06

one thing that is complicated about “fetch in route handler” is that you have to deal with the “loading” page while it works.

pesterhazy16:03:23

yeah but that's often inevitable anyway

exit216:03:30

which could just be something in the state really

pesterhazy16:03:35

plus a simple [:div] often works...

pesterhazy16:03:45

or [:div.spinner]

justinlee16:03:21

sorry yea all i mean is that sometime its easier to deal with that when all the logic is in the component instead of proxying through yet more state in the giant state object

👍 4
justinlee16:03:22

there are definite downsides to every solution i’ve dealt with for the load-on-route-change problem. unless you go full re-frame I suppose

crinklywrappr20:03:00

Is there anything like a 'reagent fiddle' app available?

justinlee20:03:22

if you find one let me know

pesterhazy22:03:12

Klipse does that

justinlee22:03:04

there doesn’t appear to be a panel to render html or am i missing something?