-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import React, { Component } from 'react' | ||
import { View, Text } from 'react-native' | ||
import { connect } from 'react-redux' | ||
import { receiveEntries, addEntry } from '../actions' | ||
import { timeToString, getDailyReminderValue } from '../utils/helpers' | ||
import { fetchCalendarResults } from '../utils/api' | ||
|
||
class History extends Component { | ||
componentDidMount () { | ||
const { dispatch } = this.props | ||
|
||
fetchCalendarResults() | ||
.then((entries) => dispatch(receiveEntries(entries))) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
xi1570-krupeshanadkat
|
||
.then(({ entries }) => { | ||
if (!entries[timeToString()]) { | ||
dispatch(addEntry({ | ||
[timeToString()]: getDailyReminderValue() | ||
})) | ||
} | ||
}) | ||
.then(() => this.setState(() => ({ready: true}))) | ||
This comment has been minimized.
Sorry, something went wrong.
iainmck29
|
||
} | ||
render() { | ||
return ( | ||
<View> | ||
<Text>{JSON.stringify(this.props)}</Text> | ||
</View> | ||
) | ||
} | ||
} | ||
|
||
function mapStateToProps (entries) { | ||
return { | ||
entries | ||
} | ||
} | ||
|
||
export default connect( | ||
mapStateToProps, | ||
)(History) |
12 comments
on commit f2f0342
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the end of the video the dummy calendar data is visible in the rendered JSON but I had to change the formatCalendarResults
function in ./utils/_calendar
because at initialization results
was !== null ("{}" !== null) thus preventing dummy data loading.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to run AsyncStorage.clear() if you submitted any prior data to the "DB". That is why results was not === null.
I ran it inside fetchCalendarResults() one time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the tips, that not only worked but saved valuable debugging time!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the 14 line of History.js, the second then
:
.then(({ entries }) => {
if (!entries[timeToString()]) {
dispatch(addEntry({
[timeToString()]: getDailyReminderValue()
}))
}
})
({ entries })
will cause a warning in my environment:
[Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_ref.entries')]
Stack trace:
components\History.js:18:15 in
node_modules\promise\setimmediate\core.js:37:14 in tryCallOne
node_modules\promise\setimmediate\core.js:123:25 in
...
If I write it in this way, the warning is gone:
.then(() => {
const { entries } = this.props
if (!entries[timeToString()]) {
dispatch(addEntry({
[timeToString()]: getDailyReminderValue()
}))
}
})
I am not very familiar with Promise. So I don't know it's my problem or something else. It looks like the argument for then
should come from Promise, but not from state or props. ({ entries })
is a destructure from props. So it leads to a warning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jerryfishzz, can you double-check it's not (entries) but ({ entries })? Forgetting to destructure is often a common cause for errors. There is no reason for entries to come from props so even if you don't get any any warning, does it work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @sanjibahmad. The problem comes from the first then
in my code. Not familiar with ES6 and Promise, I write the first then
this way:
.then((entries) => {
dispatch(receiveEntries(entries))
})
No return value from the first then
so as to be the warning in second then
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jerryfishzz, can you double-check it's not (entries) but ({ entries })? Forgetting to destructure is often a common cause for errors. There is no reason for entries to come from props so even if you don't get any any warning, does it work?
I'm not sure why we're destructuring in the second .then but not on the first one.
can anyone explain?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @sanjibahmad. The problem comes from the first
then
in my code. Not familiar with ES6 and Promise, I write the firstthen
this way:.then((entries) => { dispatch(receiveEntries(entries)) })
No return value from the first
then
so as to be the warning in secondthen
.
Hey!!!!! i found why.
did you return from the previous promise??
turns out dispatch returns a promise with the action you just submitted. thats why yo distructure.
if you dont return . the second .then() getn nothing
fetchCalendarResults()
.then(entries => {
return dispatch(receiveEntries(entries));
})
.then(({ entries }) => {
if (!entries[timeToString()]) {
dispatch(
addEntry({
[timeToString()]: getDailyReminderValue()
})
);
}
});
notice how the first .then returns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @sanjibahmad. The problem comes from the first
then
in my code. Not familiar with ES6 and Promise, I write the firstthen
this way:.then((entries) => { dispatch(receiveEntries(entries)) })
No return value from the first
then
so as to be the warning in secondthen
.Hey!!!!! i found why.
did you return from the previous promise??
turns out dispatch returns a promise with the action you just submitted. thats why yo distructure.
if you dont return . the second .then() getn nothingfetchCalendarResults() .then(entries => { return dispatch(receiveEntries(entries)); }) .then(({ entries }) => { if (!entries[timeToString()]) { dispatch( addEntry({ [timeToString()]: getDailyReminderValue() }) ); } });
notice how the first .then returns.
Not quite, dispatch does not return a promise by itself. I mean dispatch (thanks to thunks) only returns plain JS objects which is what arrives to the reducers, thunks will make sure of that.
What it is happening there is that anything that you wrap in a then is therefore 'thenable' (as long as it returns something that something will be thenable). Which means that if that something is really a Promise it will follow the pending and resolved/rejected flow and if it is not it will be returned as a resolved promise with the value of that something.
The other issue, destructuring in the second then... well there you are destructuring the response of dispatch(receiveEntries(entries)) which is:
{
type: RECEIVE_ENTRIES,
entries,
}
On the other hand fetchCalendarResults()
only returns an object with the entries, not an object with a property entries, so no, you can't do the destructuring in the first then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me, whether destructuring or not in the second then makes no difference in the output. Not sure why we need destructuring here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does anyone get this error: "TypeError: Cannot read property 'getItem' of undefined"
I think this is related to this issue:
facebook/react-native#18372
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does anyone get this error: "TypeError: Cannot read property 'getItem' of undefined"
I think this is related to this issue:
facebook/react-native#18372
I was getting the same error. But I find out that I was using the old import of AsyncStorage rather than the actual AsyncStorage library
I was mistakenly using this in my utils/api.js
import { AsyncStorage } from 'react-native'
instead of this:
import AsyncStorage from '@react-native-community/async-storage';
On line number 13 the return value of fetchCalenderResults promise is null but in the video there was so much data that was returning from this. So why I am getting null?