Skip to content

Instantly share code, notes, and snippets.

@MaximeHeckel
MaximeHeckel / Article.js
Created March 27, 2018 02:40
React sub-components Take 2 - Full sub-components example with the new context API
import React from "react";
// This creates the "Article Context" i.e. an object containing a Provider and a Consumer component
const ArticleContext = React.createContext();
// This is the Title sub-component, which is a consumer of the Article Context
const Title = () => {
return (
<ArticleContext.Consumer>
{({ title, subtitle }) => (
@MaximeHeckel
MaximeHeckel / App.js
Last active May 10, 2018 01:26
React sub-components Take 2 - Second use of a sub-component example with the new context API
import React, { Component } from "react";
import Article, { Title } from "./Article";
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>
@MaximeHeckel
MaximeHeckel / App.js
Last active April 3, 2018 01:47
React sub-components Take 2 - First use of a sub-component example with the new context API
import React, { Component } from "react";
import Article from "./Article";
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>
@MaximeHeckel
MaximeHeckel / Article.js
Last active March 27, 2018 01:58
React sub-components Take 2 - First sub-component example with the new context API
import React from "react";
// This creates the "Article Context" i.e. an object containing a Provider and a Consumer component
const ArticleContext = React.createContext();
// This is the Title sub-component, which is a consumer of the Article Context
const Title = () => {
return (
<ArticleContext.Consumer>
{({ title, subtitle }) => (
@MaximeHeckel
MaximeHeckel / App.js
Last active March 20, 2018 16:09
React subcomponents with new context API (React 16.3.0-alpha.2)
import React, { Component } from 'react';
const ArticleContext = React.createContext();
const OtherContext = React.createContext();
const OutOfContextComponent = OtherContext.Provider;
const Article = ArticleContext.Provider;
const View = ArticleContext.Consumer;
const Title = () => {
@MaximeHeckel
MaximeHeckel / Article.js
Created February 26, 2018 20:41
Sub-components IE/Edge workaround
...
const Title = () => null;
Title.displayName = 'Title';
const Subtitle = () => null;
Subtitle.displayName = 'Subtitle';
const Metadata = () => null;
Metadata.displayName = 'Metadata';
@MaximeHeckel
MaximeHeckel / Article.spec.js
Created February 1, 2018 06:48
Medium - React "sub-components" - Article test file
import React from 'react';
import { mount } from 'enzyme';
import Article from '../';
// First we declare some mocks
const Content = () => <div>[Mock] Content</div>;
const Subtitle = () => <div>[Mock] Subtitle</div>;
const Comments = () => <div>[Mock] Comments</div>;
const Metadata = () => <div>[Mock] Metadata</div>;
const Title = () => <div>[Mock] Title</div>;
@MaximeHeckel
MaximeHeckel / Article.js
Last active August 6, 2018 05:44
Medium - React "sub-components" - Article sub-components full
// @flow
import React, { Component } from 'react';
import type { Node } from 'react';
import findByType from './findByType';
import css from './styles.css';
const Title = () => null;
const Subtitle = () => null;
const Metadata = () => null;
const Content = () => null;
@MaximeHeckel
MaximeHeckel / MyArticleView.js
Last active February 1, 2018 18:33
Medium - React "sub-component" - Article usage
<Article>
<Article.Title>
My Article Title
</Article.Title>
</Article>
@MaximeHeckel
MaximeHeckel / Article.js
Last active August 17, 2018 20:02
Medium - React "sub-component" - Title sub-component
import React, { Component } from 'react';
import findByType from './findByType';
import css from './somestyle.css';
// We instantiate the Title sub-component
const Title = () => null;
class Article extends Component {
// This is the function that will take care of rendering our Title sub-component
renderTitle() {
const { children } = this.props;
// First we try to find the Title sub-component among the children of Article