Created
October 16, 2019 14:55
-
-
Save kana-sama/4300762626ebd7aa1ba86817acd5c6b9 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 * as React from "react"; | |
import * as ReactDOM from "react-dom"; | |
import { observable, action } from "mobx"; | |
import { useLocalStore, useObserver } from "mobx-react-lite"; | |
class CurrentUserModel { | |
@observable value = null; | |
@action fetch() { | |
setTimeout(() => { | |
this.value = { | |
name: `kana`, | |
age: new Date().getSeconds() | |
}; | |
}, 500); | |
} | |
} | |
const currentUserModel = new CurrentUserModel(); | |
function User({ name, age }) { | |
return ( | |
<> | |
<div>Name: {name}</div> | |
<div>Age: {age}</div> | |
</> | |
); | |
} | |
function CurrentUser() { | |
React.useEffect(() => currentUserModel.fetch(), []); | |
return useObserver(() => { | |
if (currentUserModel.value == null) { | |
return "Loading..."; | |
} else { | |
return ( | |
<User | |
name={currentUserModel.value.name} | |
age={currentUserModel.value.age} | |
></User> | |
); | |
} | |
}); | |
} | |
function App() { | |
const state = useLocalStore(() => ({ | |
isOpen: false, | |
toggle() { | |
this.isOpen = !this.isOpen; | |
} | |
})); | |
return useObserver(() => ( | |
<> | |
<button onClick={state.toggle}>toggle</button> | |
<div>{state.isOpen && <CurrentUser />}</div> | |
</> | |
)); | |
} | |
ReactDOM.render(<App />, document.getElementById("root")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment