i created an state
in constructor, and setState
in componentDidmount
, when i try to render
that state object to screen, it is throwing error, server im using is node API
, and every other things are working fine. help me to solve this issue
app.js (react)
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import axios from 'axios';
class App extends Component {
constructor(props) {
super(props);
this.State = {
messages:[]
}
}
componentDidMount() {
axios.get('/patient/billing_get')
.then((result) => {
const messages = result.data
console.log(messages);
this.setState({
messages: [...messages]
})
})
}
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
<div>{this.State}</div>
</p>
</div>
);
}
}
export default App;
route function which im calling from node API:
function billing_get(req,res) {
res.send("get method working")
}
Note : i have created this project by modifying create-react-app
, and i have added proxy
under react package.json to establish connection between node API and react
complete error message:
Objects are not valid as a React child (found: object with keys {messages}). If you meant to render a collection of children, use an array instead.
in div (at App.js:42)
in p (at App.js:40)
in div (at App.js:35)
in App (at index.js:7)
./src/index.js
src/index.js:7
4 | import App from './App';
5 | import registerServiceWorker from './registerServiceWorker';
6 |
> 7 | ReactDOM.render(<App />, document.getElementById('root'));
8 | registerServiceWorker();
9 |
10 |
This screen is visible only in development. It will not appear if the app crashes in production.
Open your browser’s developer console to further inspect this error.