In this lesson I demonstrate how to use the library MDXC to create and import React components with Markdown. MDXC converts markdown into JavaScript and supports JSX.

Additional Resources: https://github.com/jamesknelson/mdxc

 
Create a React app by using 'create-react-app':
  1. // .babelrc
  2.  
  3. {
  4. "presets": ["babel-preset-react-app"]
  5. }

Then, you can import a component from any Markdown file by prepending the filename with !babel-loader!mdx-loader!. For example:

  1. /* eslint-disable import/no-webpack-loader-syntax */
  2. import DocumentComponent from '!babel-loader!mdx-loader!../pages/index.md'

App.js

  1. /* eslint-disable import/no-webpack-loader-syntax */
  2.  
  3. import React, { Component } from "react";
  4. import logo from "./logo.svg";
  5. import "./App.css";
  6.  
  7. import HelloWorld from "!babel-loader!mdx-loader!./HelloWorld.md";
  8.  
  9. class App extends Component {
  10. render() {
  11. return (
  12. <div className="App">
  13. <header className="App-header">
  14. <img src={logo} className="App-logo" alt="logo" />
  15. <h1 className="App-title">Welcome to React</h1>
  16. </header>
  17. <p className="App-intro">
  18. To get started, edit <code>src/App.js</code> and save to reload.
  19. </p>
  20. <HelloWorld text="blah blah blah" />
  21. </div>
  22. );
  23. }
  24. }
  25.  
  26. export default App;
  1. import Bold from "./Bold"
  2.  
  3. import Italic from "!babel-loader!mdx-loader!./Italic.md"
  4.  
  5. prop text
  6.  
  7. # Heading
  8.  
  9. ## Heading
  10.  
  11. <p>{text}</p>
  12. <Bold>This text is Bold</Bold>
  13. <Italic>This text is Italic</Italic>

Bold.js:

  1. import React from "react";
  2.  
  3. export default function Bold({ children }) {
  4. return <b>{children}</b>;
  5. }

Italic.md

  1. prop children
  2.  
  3. {children}

Github

[React] Create and import React components with Markdown using MDXC的更多相关文章

  1. [React] Update State in React with Ramda's Evolve

    In this lesson we'll take a stateful React component and look at how we can refactor our setState ca ...

  2. React报错之React hook 'useState' is called conditionally

    正文从这开始~ 总览 当我们有条件地使用useState钩子时,或者在一个可能有返回值的条件之后,会产生"React hook 'useState' is called conditiona ...

  3. [React] Create & Deploy a Universal React App using Zeit Next

    In this lesson, we'll use next to create a universal React application with no configuration. We'll ...

  4. [React] Create an Animate Content Placeholder for Loading State in React

    We will create animated Content Placeholder as React component just like Facebook has when you load ...

  5. [React] Create component variations in React with styled-components and "extend"

    In this lesson, we extend the styles of a base button component to create multiple variations of but ...

  6. [React Router v4] Render Multiple Components for the Same Route

    React Router v4 allows us to render Routes as components wherever we like in our components. This ca ...

  7. [React] Use the React Effect Hook in Function Components

    Similar to the State Hook, the Effect Hook is “first-class” in React and handy for performing side e ...

  8. React.createClass vs. ES6 Class Components

    1 1 1 https://www.fullstackreact.com/articles/react-create-class-vs-es6-class-components/ React.crea ...

  9. 聊聊React高阶组件(Higher-Order Components)

    使用 react已经有不短的时间了,最近看到关于 react高阶组件的一篇文章,看了之后顿时眼前一亮,对于我这种还在新手村晃荡.一切朝着打怪升级看齐的小喽啰来说,像这种难度不是太高同时门槛也不是那么低 ...

随机推荐

  1. 什么是php?php的优缺点有哪些?与其它编程语言的优缺点?

    身为一个PHP开发者,有必要了解一下PHP的缺点,知道每种语言的优点和缺点,才能知道某种语言在什么场景下适合使用,在什么场景下不适合使用. 这个问题我曾经面试的时候遇到过,我之前没总结过,第一问大部分 ...

  2. 牛客网暑期ACM多校训练营(第六场) C Generation I(组合数学, 逆元)

    中链接: https://www.nowcoder.com/acm/contest/144/C 题意: 给定n个集合, 要求用n次操作, 第i次操作用1~m中一个数填入 i ~ n个集合中, 集合无序 ...

  3. 排序算法C语言实现——冒泡排序

    /*冒泡O(n^2)*//*原理:    比较相邻的元素.如果第一个比第二个大,就交换他们两个.    对每一对相邻元素做同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该会是最大的数 ...

  4. C# 导出Excel的示例

    Excel知识点.  一.添加引用和命名空间 添加Microsoft.Office.Interop.Excel引用,它的默认路径是C:\Program Files\Microsoft Visual S ...

  5. Java-替换字符串中的子字符串

    自顶一个repace方法 package com.tj; public class MyClass implements Cloneable { public static void main(Str ...

  6. Hive中文注释乱码解决方案(2)

    本文来自网易云社区 作者:王潘安 执行阶段 launchTask    回到Driver类的runInternal方法,看以下执行过程.在runInternal方法中,执行过程调用了execute方法 ...

  7. wordpress无法登录的解决方法

    使用wordpress建站的朋友可能会遇到wordpress管理密码,有时甚至是正确的密码,但是多次尝试输入都无法登录,并且输入用户名和输入电子邮件都无法获取密码,遇到这种情况怎么办,本文教你如何处理 ...

  8. Leetcode 363.矩形区域不超过k的最大数值和

    矩形区域不超过k的最大数值和 给定一个非空二维矩阵 matrix 和一个整数 k,找到这个矩阵内部不大于 k 的最大矩形和. 示例: 输入: matrix = [[1,0,1],[0,-2,3]], ...

  9. 【HTML/XML 1】HTML 速成总结

    导读:反反复复的看完了HTML的速成材料,前面学习了牛腩,所以这块知识只能是作为一种旧知识的复习.和机房重构时的SQLHelper一样,刚开始都是稀里糊涂的用了,后面系统的学习后,才知道为什么要那样用 ...

  10. 安卓ImageView.src设置图片拉伸、填满控件的方法

    代码改变世界 安卓ImageView.src设置图片拉伸.填满控件的方法 需要给你的ImageView布局加上Android:adjustViewBounds="true"