2017-09-08 7 views
-1

React.ComponentのcomponentDidMountメソッドを利用できるように、これを機能コンポーネントからクラスコンポーネントに変換する必要があります。React関数コンポーネントをクラスコンポーネントに変換する際の問題

const receivedStyle = { 
    marginRight: '0', 
    marginLeft: 'auto', 
}; 
const receivedBubble = { 
    backgroundColor: '#709AFF', 
    color: 'white', 
}; 
const receivedDate = { 
    marginRight: '0', 
    marginLeft: 'auto', 
}; 

const MessageBubble = ({ message, received }) => (
    <div className="message-bubble" style={received ? receivedStyle : null}> 
    <div className="bubble" style={received ? receivedBubble: null}> 
     {message.message} 
    </div> 
    <span className="date" style={received ? receivedDate: null}>{Moment(message.timestamp).startOf('minute').fromNow()}</span> 
    </div> 
); 

export default MessageBubble; 
+1

何が問題なのですか?単にui部分をレンダリングメソッドの中に置き、 'this.props'を使って小道具の値にアクセスしてください。 [**機能コンポーネントをクラスコンポーネントに変換する方法**]のドキュメントを確認してください(https://facebook.github.io/react/docs/components-and-props.html#functional-and-class-components) –

答えて

0

私は何が問題なのか分かりません。とにかく、ここに行く:

import React, { Component } from 'react' 

const receivedStyle = { 
    marginRight: '0', 
    marginLeft: 'auto', 
} 

const receivedBubble = { 
    backgroundColor: '#709AFF', 
    color: 'white', 
} 

const receivedDate = { 
    marginRight: '0', 
    marginLeft: 'auto', 
} 

export default class MessageBubble extends Component { 
    componentDidMount() { 
    ... 
    } 

    render() { 
    const { message, received } = this.props 

    return (
     <div className="message-bubble" style={received ? receivedStyle : null}> 
     <div className="bubble" style={received ? receivedBubble: null}> 
      {message.message} 
     </div> 
     <span 
      className="date" 
      style={received ? receivedDate: null} 
     > 
      {Moment(message.timestamp).startOf('minute').fromNow()} 
     </span> 
     </div> 
    ) 
    } 
} 
+2

それは簡単でもない、あなたは 'class'キーワードを見落とし、Componentのインポート方法について忘れてしまった。 –

+0

あなたはそれを持っています:) – mersocarlin

+0

receivedStyle、receivedBubble、receivedDateのコードはどこに書くべきですか? –

関連する問題