【发布时间】:2016-07-21 23:01:35
【问题描述】:
我正在使用 Redux 在 React 中构建一个站点,我的目标是拥有一个 /pages 文件夹,其中包含代表页面内容的降价文件。
我有一个 React 容器(智能)和一个 React 组件(哑),它们将用于呈现内容。我的想法是我将 Markdown 文件导入到父容器中,然后将它们传递给子组件以在浏览器中呈现它们。我知道我需要使用 markdown-it 之类的东西将 MD 转换为 HTML,也许我需要在子组件中使用它。
我的问题是:
1) 如何使用 this.props 将 markdown 文件从父级传递给子级? 2)我在正确的轨道上吗?这是正确的做法吗?
这是我的组件:
页面父容器
import React from 'react'
import SamplePage from './../components/SamplePage'
export default class Page extends React.Component {
constructor(props) {
super(props)
}
render() {
return (
<section>
<SamplePage />
</section>
)
}
}
SamplePage 子组件
import React from 'react'
import { Link } from 'react-router'
export default class SamplePage extends React.Component {
constructor(props) {
super(props)
}
render() {
return (
<div className="page-container">
<Link to="/" className="previous-link">Go Back Home</Link>
// I want to render the MD here using {this.props.markdown}
</div>
)
}
}
【问题讨论】:
-
在渲染markdown方面,已经有很多解决方案了。例如:github.com/rexxars/react-markdown
-
对,渲染markdown的解决方案有很多。我的问题是我不完全了解如何从 /page 文件夹中检索 markdown 并将其作为属性传递,然后使用 markdown-it 之类的东西在组件中呈现它。
-
您可以发出 HTTP 请求来获取文件内容。例如facebook.github.io/react/tips/initial-ajax.html
标签: reactjs