我需要將其從功能組件轉換爲類組件,以便我可以利用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;
有什麼問題?只需將ui部分放在render方法中並使用'this.props'來訪問道具值。檢查文檔[**如何將功能組件轉換爲類組件**](https://facebook.github.io/react/docs/components-and-props.html#functional-and-class-components) –