React Base Fiddle (JSX)

Starting point for creating JSFiddles with React.

by Patrick Gordon

HTML

<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="container">
  <!-- This element's contents will be replaced with your component. -->
</div>

Babel + JSX

class PostList extends React.Component {
	render() {
		const { posts } = this.props;
		
		return (
			<ul>
				{posts.map(post => {
					 return (
						 <li key={post.id}>
							 {post.title}
						 </li>
					 )
				})}
			</ul>			
		);
	}
}

class Posts extends React.Component {
	state = {
		posts: []
	}

	async componentDidMount() {
		const fetchConfig = {
			method: "GET",
			headers: new Headers({ "Content-Type": "application/json" }),
			mode: "cors"
		}

		const response = await fetch("https://jsonplaceholder.typicode.com/posts/", fetchConfig);
		
		if (response.ok) {
			const posts = await response.json();
			this.setState({ posts });
		} else {
			console.log("error!", error);
		}
	}

	render() {
		const { posts } = this.state;

		return (
			<PostList posts={posts} />
		)
	}
}

ReactDOM.render(
  <Posts />,
  document.getElementById('container')
);