Created
April 1, 2018 15:20
-
-
Save nthock/284168082ca14697174c48d8fa26b10f to your computer and use it in GitHub Desktop.
Simple setup of Rails API + React Frontend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import request from 'superagent'; | |
import { compose, withState, withHandlers, lifecycle } from 'recompose'; | |
const Dashboard = (props) => { | |
const { winnie, onSubmit, users } = props; | |
return ( | |
<div> | |
<div>This is Dashboard - {winnie}</div> | |
<button onClick={() => onSubmit()}>Click Me</button> | |
{ users.map((user) => { | |
return ( | |
<div key={`user-${user.id}`}> | |
<p>{user.name} - {user.age}</p> | |
</div> | |
) | |
})} | |
</div> | |
); | |
}; | |
const enhance = compose( | |
withState('users', 'setUsers', []), | |
withState('winnie', 'setWinnie', 'ng thiam hock'), | |
withHandlers({ | |
onSubmit: props => () => { | |
const { setWinnie } = props; | |
setWinnie('Winnie said I love you too'); | |
} | |
}), | |
lifecycle({ | |
componentDidMount() { | |
const { setUsers } = this.props; | |
request | |
.get('http://localhost:3001/users') | |
.end((err, res) => { | |
setUsers(res.body); | |
}); | |
} | |
}) | |
); | |
export default enhance(Dashboard); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersController < ApplicationController | |
def index | |
@users = [ | |
{"id": 1, "name": "John", "age": 45}, | |
{"id": 2, "name": "James", "age": 20}, | |
{"id": 3, "name": "Thiam Hock", "age": 21}, | |
{"id": 4, "name": "Joanne", "age": 80} | |
] | |
render json: @users | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment