I took over a React Native project from somebody else, this project is quite old (2 years) and I came across the following:
Home.js Component: (I simplified it)
export let customersData = null;
export default class Home extends Component {
render() {
return (
<JumboButton
onPress={() => {
this.props.navigator.push({
component: CustomerSearch
});
}}
>
);
}
_getAllCustomers(limit, sortAttr, order) {
apiCall.(apiUrl, {
...
}).then((responseData) => {
const customersDataAll = responseData.data;
customersData = customersDataAll.filter((f) => {
return f.lastname !== ''
});
});
}
}
So within the Home-Component, customersData is filled with data. Also the component CustomerSearch is called and within CustomerSearch I found this:
CustomerSearch.js:
import {customersData} from './Home';
export default class CustomerSearch extends Component {
constructor(props) {
super(props);
this.ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: this.ds.cloneWithRows(customersData),
};
}
}
Two things are a bit weird for me:
Is it correct to set
customersDataand notthis.customersDatainside the callback of the api call?Currently I get this error https://d.pr/i/IUzxdf "Cannot convert undefined or null to object" and I assume that this is because of the data import of
customersDatain CustomerSearch.js. Is the place where I need to look? Btw is there any chance that react tells me the exact line and file where this error occurs?
Thanks for your help!
CustomerSearchcomponent? Also, I don' get it why don't you set a state inHomecomponent populate the data there and renderCustomerSearchcomponent in Home, then pass the data as a prop? How doesCustomerSearchcomponent knows when the data updated if you import it like that?