• [React] Create and import React components with Markdown using MDXC


    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':
    // .babelrc
    
    {
        "presets": ["babel-preset-react-app"]
    }

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

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

    App.js

    /* eslint-disable import/no-webpack-loader-syntax */
    
    import React, { Component } from "react";
    import logo from "./logo.svg";
    import "./App.css";
    
    import HelloWorld from "!babel-loader!mdx-loader!./HelloWorld.md";
    
    class App extends Component {
      render() {
        return (
          <div className="App">
            <header className="App-header">
              <img src={logo} className="App-logo" alt="logo" />
              <h1 className="App-title">Welcome to React</h1>
            </header>
            <p className="App-intro">
              To get started, edit <code>src/App.js</code> and save to reload.
            </p>
            <HelloWorld text="blah blah blah" />
          </div>
        );
      }
    }
    
    export default App;
    import Bold from "./Bold"
    
    import Italic from "!babel-loader!mdx-loader!./Italic.md"
    
    prop text
    
    # Heading 1
    
    ## Heading 2
    
    <p>{text}</p>
    <Bold>This text is Bold</Bold>
    <Italic>This text is Italic</Italic>

    Bold.js:

    import React from "react";
    
    export default function Bold({ children }) {
      return <b>{children}</b>;
    }

    Italic.md

    prop children
    
    {children}

    Github

  • 相关阅读:
    最小费用流(km的另一种使用思路)
    最小费用流(km的另一种使用思路)
    Python 字符串格式化
    Python字符串运算符
    Python 字符串连接
    Python 访问字符串中的值
    虚拟纹理与几何图像技术
    深度学习与传统图像识别
    ASML光刻机PK 原子弹,难度?
    L4自动驾驶技术
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9828093.html
Copyright © 2020-2023  润新知