I'm fetching the data from the following call which returns an object:
function getPersonList() {
const api = 'myapistring';
axios.get(api).then(res => {
console.log(res);
}).catch(err => console.log(err));
}
1 - However when I hit my componentDidMount. The promise is breaking and I don't know why.
2- Also since the response is an object, am I doing something wrong by setting the initial state to an empty [ ]? -I'm not sure what's the syntax to set it as an object.
const App = React.createClass({
getInitialState() {
return {
personList: [],
visiblePersonList: []
};
},
componentDidMount() {
console.log(getPersonList(response));
getPersonList().then((data) =>
this.setState({
data,
visiblePersonList: data
}));
//return getPersonList;
},
.....
Thank you everyone!
[]) as initial values that will soon be objects, it's better to assign them with empty objects:{}. See docs. - rishatList. - Daniel A. White