Skip to content

Instantly share code, notes, and snippets.

@talentlessguy
Created December 30, 2018 12:30
Show Gist options
  • Save talentlessguy/8f4f1b245846b8f837f735f273e9fb0b to your computer and use it in GitHub Desktop.
Save talentlessguy/8f4f1b245846b8f837f735f273e9fb0b to your computer and use it in GitHub Desktop.
const app = require('express')()
app.use(require('cors')())
app.get('/verycoolguy/posts/1',
(req, res) => res.sendFile(__dirname + '/post.json')
)
app.get('/cdn/verycoolguy/posts/1/img.jpg',
(req, res) => res.sendFile(__dirname + '/img.jpg'))
app.listen(4000)
import React from 'react'
import { hydrate } from 'react-dom'
import Post from './Post'
hydrate(
<div>
<Post/>
</div>, document.getElementById('root')
)
import React, { Component } from 'react'
import axios from 'axios'
import ReactMarkdown from 'react-markdown'
import Img from 'react-image'
export default class Post extends Component {
constructor () {
super()
this.state = {
content: '',
likes: '',
comments: []
}
}
componentDidMount(){
axios.get('http://localhost:4000/verycoolguy/posts/1')
.then(res => {
this.setState({
text: res.data.text,
likes: res.data.likes,
comments: res.data.comments,
image: res.data.image
});
console.log(this.state.image)
})
}
render = () => (
<div>
<Img src={this.state.image}/>
<ReactMarkdown source={this.state.text}/>
<span>Likes: {this.state.likes}</span>
<div className="comments">
{this.state.comments.map(e => (
<div>
<p><b>{e.author}</b> {e.text}</p>
</div>
))}
</div>
</div>
)
}
{
"image": "localhost:4000/cdn/verycoolguy/posts/1/img.jpg",
"text": "**bold**",
"likes": "30",
"comments": [
{
"author": "supadupasuser",
"text": "md rocks",
"likes": "10"
},
{
"author": "anothercooluser",
"text": "html is better",
"likes": "0"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment