Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created October 5, 2018 01:22
Show Gist options
  • Select an option

  • Save prof3ssorSt3v3/6cfc8a272d17c2d30341222bc068870f to your computer and use it in GitHub Desktop.

Select an option

Save prof3ssorSt3v3/6cfc8a272d17c2d30341222bc068870f to your computer and use it in GitHub Desktop.
import React, {Component} from 'react';
import '../styles/Message.css';
export default class Message extends Component{
constructor(props){
super(props);
this.state = {
id: props.msg.id,
title: props.msg.title,
text: props.msg.text,
status: props.msg.status
}
//this will only run on initial creation of the Component, not updates
}
componentWillUpdate(){
//React will complain if you try to set state here
//this.setState({id:23423});
}
componentDidUpdate(){
//React will complain if you try to set state here
}
toggle = (ev) =>{
let msg = {...this.state};
msg.status = (msg.status === 'read')?'not-read':'read';
this.setState({...msg})
this.props.toggleRead(msg);
}
render(){
let label = (this.state.status === 'not-read')?'Mark As Read':'Mark As Unread';
return (
<div className={ this.props.msg.status + ' message-box' } >
<h2>{this.props.msg.title}</h2>
<p>{this.props.msg.text}</p>
<p><button onClick={this.toggle}>{label}</button></p>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment