Last active
June 18, 2018 05:25
-
-
Save sijad/47365ee84e11eced6e322e081b083f65 to your computer and use it in GitHub Desktop.
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 { Model, find, findAll } from 'react-jsonapi'; | |
class User extends Model { | |
name: string; | |
email: string; | |
createdAt: Date; | |
} | |
@find(User) | |
@findAll(User, { | |
// other options | |
name: 'usersQuery', // prop name | |
cachePolicy: 'network', // always send a new request to server | |
}) | |
class MyComponent extends Component { | |
componentDidUpdate() { | |
// Find(User) query result | |
const { | |
loading, | |
result: user, | |
} = this.props.data; | |
if (!loading) { | |
user.name = 'new name'; | |
user.save(); | |
} | |
} | |
render() { | |
const { | |
loading, // true if users query still loading | |
result: users, // will contains [User] | |
error, | |
// etc | |
} = this.props.users; | |
if (loading) { | |
return <Loading /> | |
} | |
return users.map(user => <DisplayUser key={user.id} user={user} />); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment