Skip to content

Instantly share code, notes, and snippets.

@alonbardavid
alonbardavid / gist.js
Created October 9, 2019 17:50
Why use mobx gist 3b
function App(){
return <PostsContext.Provider>
<NotificationsContext.Provider>
<AuthenticationContext.Provider>
... ad nauseum
</AuthenticationContext.Provider>
</NotificationsContext.Provider>
</PostsContext.Provider>
}
@alonbardavid
alonbardavid / gist.js
Created October 9, 2019 17:48
Why use mobx gist9
class Form {
@observable
email = ""
@observable
password = ""
@computed
get emailError(){
if (this.email && this.email.length >0 &&
this.email.indexOf("@") >= 0){
@alonbardavid
alonbardavid / gist.js
Created October 9, 2019 17:46
Why use mobx gist 9
const ResourceContext = React.createContext();
function useResource(){
const [resource,setResource] = React.useState({});
return {
resource,
requestResource(){
const interval = setInterval(()=>({
// this is a contrived example for brevity, I know it's not
// how you'll do it and that it won't work since resource is
// always the original value
@alonbardavid
alonbardavid / gist.js
Created October 9, 2019 17:45
Why use mobx gist8
// resource-view.js
const resourceById = createSelector([
(state,props)=>state.resources[props.resourceId]
],(resource)=>resource)
const mapStateToProps = (state,ownProps) => ({
...resourceById(state,ownProps)
})
@alonbardavid
alonbardavid / gist.js
Created October 9, 2019 17:45
Why use mobx gist6
//resource-view.js
function ResourceView(props) {
const {loading,resource,error} = props;
return loading? <loader/>:<ResourceItem resource={resource}/>
}
const mapStateToProps = (state,ownProps) => ({
...state.resources[ownProps.resourceId]
})
export default connect(mapStateToProps)(ResourceView)
@alonbardavid
alonbardavid / gist.js
Created October 9, 2019 17:43
Why use mobx gist 5
function App(){
return <PostsContext.Provider>
<NotificationsContext.Provider>
<AuthenticationContext.Provider>
... ad nauseum
</AuthenticationContext.Provider>
</NotificationsContext.Provider>
</PostsContext.Provider>
}
@alonbardavid
alonbardavid / gist.js
Created October 9, 2019 17:43
Why use mobx gist4
const ResourceContext = React.createContext();
function useResource(){
const [resource,setResource] = React.useState({});
return {
resource,
requestResource(){
setResource({loading:true});
return fetch(`url/resource/${resourceId}`)
.then(response=>response.json())
.then(json=>setResource({loading:false,resource:json}))
@alonbardavid
alonbardavid / gist.js
Created October 9, 2019 17:42
Why use mobx gist 3
//resource-view.js
function ResourceView(props) {
const {loading,resource,error} = props;
return loading? <loader/>:<ResourceItem resource={resource}/>
}
const mapStateToProps = (state,ownProps) => ({
...state.resources[ownProps.resourceId]
})
export default connect(mapStateToProps)(ResourceView)
@alonbardavid
alonbardavid / gist.js
Created October 9, 2019 17:42
Why use mobx gist 2
class ResourceStore {
@observable
resources = {};
@action("REQUEST_RESOURCE")
requestResource(id) {
this.resources[id] = {loading:true};
return fetch(`url/resource/${id}`)
.then(response=>response.json())
.then(action("REQUEST_RESOURCE_SUCCESS",
@alonbardavid
alonbardavid / gist.js
Created October 9, 2019 17:41
Why use mobx gist 1
// in actions.js
// these are action creators
export const requestResource= (resourceId)=>({
type: 'REQUEST_RESOURCE',
resourceId
})
export const requestResourceSuccess = (resourceId,payload)=>({
type: 'REQUEST_RESOURCE_SUCCESS',
resourceId,
payload