I seem to be struggling with what should be a simple issue regarding setting State. I'm learning React so I did this across several steps. Initially I setup my components so they are setting fields using props (e.g. this.propsoverview[0].ProfileImg) which was handed down State from Overview Pane, which was initially set using a this.SetState call in componentWillMount where data was pulled from a static file.
Next I worked on adding a function to pull the data in a more dynamic way (i.e. getPatient function). I'm calling this function from the componentWillMount and I am able to do a console.log to output the JSON as a string (using JSON.stringify). So I know the JSON is being returned.
Where I'm struggling is setting the State in componnetWillMount using the returned JSON from my getPatient call. I get the error Uncaught (in prmise) Type Error: Cannot read property 'setState' of undefined on the 4th line of my code (i.e. this.setState...)
Below is my code...
componentWillMount() {
getPatient().then(function(result) {
//console.log(JSON.stringify(result));
this.setState({PATIENT: result})
})
function getPatient() {
const urlGetPatient = 'url_that_gets_patient_here';
return fetch(urlGetPatient).then(function(response) {
return response.json();
}).then(function(json) {
return json;
});
}
render() {
return (
<App>
…
<OverviewPane overview={this.state.PATIENT} />
…
</App>
}
class OverviewPane extends React.Component {
constructor(props) {
super(props);
autoBind(this);
}
render () {
return (
<table>
<tbody>
<tr>
<td><Image src={this.props.overview[0].ProfileImg}/></td>
</tr>
</tbody>
</table>
);
}
}
Any help would really be appreciated.