Skip to content

Instantly share code, notes, and snippets.

@MaximeHeckel
Last active April 3, 2018 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaximeHeckel/14aeb7d932bcab6b9b4ba04c0e67fd39 to your computer and use it in GitHub Desktop.
Save MaximeHeckel/14aeb7d932bcab6b9b4ba04c0e67fd39 to your computer and use it in GitHub Desktop.
React sub-components Take 2 - Full sub-components example with the new context API
import React, { Component } from "react";
import Article from "./Article";
const text = `
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
`;
class App extends Component {
constructor() {
this.state = {
value: {
title: <h1>React sub-components with</h1>,
subtitle: (
<div>Lets make simpler and more flexible React components</div>
),
author: "Maxime Heckel",
date: <i>April 2018</i>,
content: <p>{text}</p>,
},
};
}
render() {
const { value } = this.state
return (
<Article value={value}>
<Article.Title />
{/* here we can see that wrapping the metadata sub-component is now possible thanks to contexts*/}
<div style={{ width: "500px", margin: "80px auto" }}>
<Article.Metadata />
</div>
<Article.Content />
</Article>
);
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment