2017-11-29 22 views
0
import React, { Component } from 'react'; 
import './Imprint.scss'; 
import ReactMarkdown from 'react-markdown'; 
import ContentSection from '../ContentSection'; 
import axios from 'axios'; 
import { IMPRINT_URL } from '../../auth_axios'; 

class Imprint extends Component { 
    constructor(props) { 
    super(props); 

    this.state = { 
     imprintText: null, 
    } 
    } 
    componentDidMount() { 
    this.fetchImprint(); 
    } 
    render() { 
    const { 
     imprintText 
    } = this.state; 
    return (
     <section className="bsl-imprint row"> 
     <ContentSection> 
      <div className="col-xs-12 col-md-6 col-md-offset-3"> 
      <h2 className="bs-main-heading">Imprint</h2> 
      <p> 
       <ReactMarkdown source={imprintText}/> 
      </p> 
      </div> 
     </ContentSection> 
     </section> 
    ); 
    } 
    fetchImprint() { 
    axios.get(IMPRINT_URL) 
     .then(res => { 
     console.log(res) 
     this.setState({imprintText: res.data}) 
     }) 
     .catch(err => { 
     console.log(err); 
     }); 
    } 
} 

export default Imprint; 

React 15.4、Redux 3.7とBabelを使用してテキストを表示しています。そこで、私はレンダリングしようとしていますimprintTextReactMarkdownタグはコンポーネント内にありますが、コンテンツの代わりにプレーンなHTMLコードを表示しています。私はそれがどのように見えるかを示すスクリーンショットを提供します。何か案は??Reactコンポーネントでコンテンツがレンダリングされない

SCREEN_SHOT

+0

私はimprintTextを文字列と見なします。使用する前に有効なhtmlに変換してください。それを試してください – stack26

+0

@ stack26いいえReactMarkdownは 'source' propを文字列として要求します – Dane

答えて

0

あなたの 'imprintTextは' 文字列あなたは以下の方法で行う必要があり、

dangerouslySetInnerHTML={{ __html: this.state.imprintText}} 

例の場合:react-markdown docsによると

<div dangerouslySetInnerHTML={{ __html: this.state.imprintText }} /> 
+0

素晴らしい!あなたは** dangerouslySetInnerHTML **のための良いドキュメントを知っていますか?ありがとう! – Nukio

+0

https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtmlから反応するドキュメント自体。 –

1

、あなたがする必要はありescapeHtml小物をfalseに設定して、<ReactMarkdown>コンポーネントはすなわち、同様にHTMLをレンダリングします。:

<ReactMarkdown source={imprintText} escapeHtml={false}/> 

それはnot generally recommended to use dangerouslySetInnerHTMLです。また、imprintTextには<head>タグと<body>タグがすべて必要なわけではなく、<div>タグだけで十分でしょう。

関連する問題