2017-08-30 9 views
1

私は、コンポーネント内のタグを置き換える最善の方法を理解しようとしています。電子メールテンプレートのタグを置き換えます

John, 

www.test.com 

You can sign in with your email address ([email protected]) 
Your password is: 
supersecurepassword 

Thanks! 

Bob Law's Law Blog 
:私は、ユーザーの種類として更新し、ライブプレビューウィンドウで、これを表示したい

export default class Template extends Component { 
    componentWillMount() { 
    this.setState({ 
     mergeTags: { 
     '*|FIRSTNAME|*': 'John', 
     '*|LINK|*': 'www.test.com', 
     '*|EMAIL|*': '[email protected]', 
     '*|PASSWORD|*': 'supersecurepassword', 
     '*|SIGNATURE|*': 'Bob Law's Law Blog' 
     } 
    }) 
    } 

:だから、次のテキスト...

*|FIRSTNAME|*, 

*|LINK|* 

You can sign in with your email address (*|EMAIL|*) 
Your password is: 
*|PASSWORD|* 

Thanks! 

*|SIGNATURE|* 

、これを与えられました

+0

テンプレートテキスト(タグ付きのテキスト)は、コンポーネントでHTMLとしてハードコードされていますか?それとも、小道具としての純粋なテキストとして伝えられていますか? – jonahe

+0

私はあなたのコードがどのように見えるかについていくつかの仮定をして答えを投稿しました。それは何かを解決しましたか?そうでない場合は、Templateコンポーネントの外観についてさらに詳しく説明できます。 – jonahe

答えて

1

あなたの正確な設定はわかりませんが、このようなものが動作するはずです。ここ JSFiddleデモhttps://jsfiddle.net/jonahe/zrrfyxh7/

class Parent extends React.Component { 
    constructor(props) { 
    super(props); 
    this.state = { 
     '*|FIRSTNAME|*': 'John', 
     '*|LINK|*': 'www.test.com', 
     '*|EMAIL|*': '[email protected]', 
     '*|PASSWORD|*': 'supersecurepassword', 
     '*|SIGNATURE|*': "Bob Law's Law Blog" 
    }; 
    } 
    // use componentDidMount, or else the ref thing below won't work. 
    componentDidMount() { 
    let innerText = this.templateElement.innerHTML; 
    // loop through all the properties/"tags" 
    for(let tagKey in this.state) { 
     // replace the occurence of the tag key string, with the tag value in this.state 
     innerText = innerText.replace(tagKey, this.state[tagKey]) 
    } 
    this.templateElement.innerHTML = innerText; 

    } 
    render() { 
    // get a reference to the element that contains the raw template 
    return <p ref={ template => this.templateElement = template }> 
     *|FIRSTNAME|*, 
     *|LINK|* 

     You can sign in with your email address (*|EMAIL|*) 
     Your password is: 
     *|PASSWORD|* 

     Thanks! 

     *|SIGNATURE|* 
    </p>; 
    } 
} 

お役に立てば幸いです。何かが不明な場合は教えてください。

:テンプレートが純粋なテキストではなくHTMLのような、よりリアクション的な例を追加しました。 JSFiddlehttps://jsfiddle.net/jonahe/539g38kc/

+0

お返事ありがとうございます!ユーザーは実際にテキストのリアルタイムの例をカスタマイズして取得できる必要があります。したがって、カスタマイズ可能なテキストフィールドで、その下にライブプレビューウィンドウが表示されます。私はいくつかの進歩を行う場合、現在それを嫌うので、私は更新されます。 – pitchdiesel

+1

問題ありません。私は自分の答えを編集し、テンプレートが純粋なテキストである必要がない場合(HTMLのように定義されていれば)、それをどうやって行うことができるかの別の例を追加しました。この新しい例は、典型的なReactコンポーネントがどのように機能するかに似ており、記述したユースケースに非常に適しています。 – jonahe

関連する問題