Closed mhaagens closed 7 years ago
The code you have:
export const getWeather = (effects, coords) => axios.get("your-url")
.then(response => hardUpdate({ weather: response.data.main }))
is equivalent to:
export const getWeather = (effects, coords) => axios.get("your-url")
.then(response => () => state => Object.assign({}, state, { weather: response.data.main }))
Notice the extra () =>
in there. hardUpdate
is really meant for very simple state updates.
What you probably want is something that merges the new data into state:
const mergeIntoState = dataToMerge => state => Object.assign({}, state, dataToMerge);
Then, you could use that the way you (probably) want to:
export const getWeather = (effects, coords) => axios.get("your-url")
.then(response => mergeIntoState({ weather: response.data.main }))
I want to have a helper like this shipped as part of the library - the hard part is in finding a good name for it. Even hardUpdate
and softUpdate
aren't necessarily the best names, but I had a hard time coming up with better ones.
I'm guessing this is more of an issue with me writing the wrong code than the library, I'm just hoping to "get it", because I love the way it's laid out.
What I'm trying to figure out is how to write this;
export const getWeather = (effects, coords) => axios.get(`APIURL`).then(({ data: { main: weather } }) => state => Object.assign({}, state, {weather}) );
using softUpdate within the axios return.
I know this isn't really an issue with the library, so I'm sorry for posting it here, but would appreciate any pointers.
softUpdate
isn't the right tool for the job in this case. It's really only intended for synchronous updates. Naming things is hard! Instead of using softUpdate
, use the mergeIntoState
helper. It's now a baked-in helper, there are some relevant docs in the README, and it'll be released onto npm
shortly.
If you still feel stuck, feel free to reach out to me on twitter and we can discuss in a DM.
I've been spending way too long on this, is there some reason this doesn't work?
export const getWeather = (effects, coords) => axios.get(`http://api.openweathermap.org/data/2.5/weather?lat=${coords[1]}&lon=${coords[0]}&units=metric&appid=APIKEY`).then(response => hardUpdate({ weather: response.data.main }) );