This kind of problems arise normally in React, nothing to worry about. I have modified your code a bit, and ran in my own environment and it works just fine.
Here are a few of the suggestions I want to make in order to correct your React code. First of all, JSX needs to be wrapped in small brackets before a return statement, otherwise you will face automatic semicolon insertion; read
this[
^] for more on that.
So most of your code would become from,
render() {
return <div>
<p>{this.state.employees.Name}</p>
</div>;
}
To,
render() {
return (<div>
<p>{this.state.employees.Name}</p>
</div>);
}
This will solve issues with the proper object return. Secondly, if you face issues such as "Unexpected token '<'", that means that your JavaScript is not being handled as ES6 and JSX won't work there. You need to apply
type="text/babel"
to your script elements that are importing the React code. Such as,
<script type="text/babel">
class Employees...
ReactDOM...
</script>
This will help browsers parse the content and make sure it is valid. Here is the code that I am currently using for your application and it renders the employees' names.
import React from "react";
import Employee from "./Employee";
class Employees extends React.Component {
constructor(props) {
super(props);
this.state = { employees: [ { Name: "Someone" }, { Name: "Else" }, { Name: "Working" }, { Name: "Here" } ] };
}
render() {
return (
<div>
{
this.state.employees.map(function (employee) {
return (<Employee employee={employee} />)
})
}
</div>);
}
}
export default Employees;
import React from "react";
class Employee extends React.Component {
constructor(props) {
super(props);
this.state = { employee: props.employee };
}
render() {
return (<div>
<p>{this.state.employee.Name}</p>
</div>);
}
}
export default Employee;
function App() {
return (<Employees />);
}
ReactDOM.render(<App />, document.getElementById('root'));
This is the flow of the application that I have used and it has worked just perfectly. You can also try out the same features on CodePen as their React editor normally works just fine. https://codepen.io/pen/
Oh, and the data loading feature from Ajax needs to be implemented aside from React,
React is not a framework. You can try jQuery Ajax, to download the content from React and then push that to the React components state.
See
this link.[
^]