// ...
import { MobXProviderContext } from "mobx-react"
export const PlayersScreen = observer((props) => {
const rootStore = React.useContext(MobXProviderContext).RootStore
// access properties
rootStore.status
// iterate through arrays
const teamNames = rootStore.teams.map(team => team.name )
// fire actions
rootStore.addTeam(newTeam)
// access views
rootStore.sortedTeams
// ...
})
import { types } from "mobx-state-tree"
import { City } from "../city"
export const Team = types.model({
id: types.identifier,
city: types.reference(City),
region: types.string,
name: types.string,
abbrev: types.string,
imgURL: types.string,
capacity: types.number,
strategy: types.enumeration("strategy", [
"rebuilding", "contending"
]),
})
import { types, getEnv, flow } from "mobx-state-tree"
import { Team } from "./team"
export const RootStore = types
.model("RootStore", {
teams: types.array(Team),
status: types.enumeration(["pending", "loading", "done", "error"]), "pending"),
})
.actions(self => ({
setTeams: teams => (self.teams = teams),
setStatus: newStatus => (self.status = newStatus),
getAll: flow(function* () {
self.status = "loading"
const { teams } = yield getEnv(self).api.get()
self.teams = teams
self.status = "done"
})
}))
import * as React from "react"
import { FlatList } from "react-native"
import { observer, MobXProviderContext } from "mobx-react"
import { TeamRow } from "./team-row"
export const TeamsScreen = observer((props) => {
const rootStore = React.useContext(MobXProviderContext).RootStore
const { teams } = rootStore
return (
<FlatList
data={teams}
keyExtractor={t => t.id}
renderItem={({ item }) =>
<TeamRow imgURL={item.imgURL} name={item.name} />
}
/>
)
})
// Display a "warmer" to ask for a permission type
warm: flow(function* (permissionType) {
self.permissionType = permissionType
// check if already permitted and succeed if so
let permission = yield check(permissionType)
if (permission === "authorized") return true
// otherwise show warmer
self.navigateTo("warmer")
// wait for the warmer to dismiss
yield when(() => !self.isWarmerActive)
// now check permissions again
permission = yield check(permissionType)
return permission === "authorized"
})
import { types, getEnv, flow } from "mobx-state-tree"
import { Team } from "./team"
export const RootStore = types
.model("RootStore", {
teams: types.array(Team),
status: types.enumeration(["pending", "loading", "done", "error"]), "pending"),
})
.actions(self => ({
// ...
})
.views(self => ({
get sortedTeams() {
return self.teams.sort((a, b) => {
return (a.city < b.city) ? -1 : 1
)
}
})
import { types, onSnapshot, getRoot } from 'mobx-state-tree'
import { Team } from '../models/team'
import { User } from '../models/user'
export const NavigationStore = types
.model('NavigationStore', {
profileScreenParams: types.model('profileScreenParams', {
user: types.maybe(types.safeReference(User))
}),
teamScreenParams: types.model('TeamScreenParams', {
team: types.maybe(types.safeReference(Team))
})
})
// instead of this
navigation.navigate('ProfileScreen', { user: someUser }))
// you'd do this
navigationStore.setUser(someUser)
navigation.navigate('ProfileScreen')
// ... other imports
import { useNavigation } from 'react-navigation-hooks'
const MyLinkButton = (props) => {
const { navigate } = useNavigation()
return <View><Button onPress={() => navigate("Home")} /></View>
}
import React, { useState } from "react"
import { View } from "react-native"
import { useNavigation } from "react-navigation-hooks"
import { observer, MobXProviderContext } from "mobx-react"
import { PlayerList } from "./player-list"
export const PlayersScreen = observer(props => {
const rootStore = React.useContext(MobXProviderContext).RootStore
const { players } = rootStore
const [currentPlayer, setPlayer] = useState(undefined)
const navigation = useNavigation()
const goBack = () => navigation.goBack(null)
return (
<View testID="PlayersScreen">
<PlayerList currentPlayer={currentPlayer} players={players} goBack={goBack} />
</View>
)
})
import React, { useState } from "react"
import { View } from "react-native"
import { useNavigation } from "react-navigation-hooks"
import { observer, MobXProviderContext } from "mobx-react"
import { PlayerList } from "./player-list"
export const PlayersScreen = observer(props => {
const rootStore = React.useContext(MobXProviderContext).RootStore
const { players } = rootStore
const [currentPlayer, setPlayer] = useState(undefined)
const navigation = useNavigation()
const goBack = () => navigation.goBack(null)
return (
<View testID="PlayersScreen">
<PlayerList currentPlayer={currentPlayer} players={players} goBack={goBack} />
</View>
)
})
import React, { useState } from "react"
import { View } from "react-native"
import { useNavigation } from "react-navigation-hooks"
import { observer, MobXProviderContext } from "mobx-react"
import { PlayerList } from "./player-list"
export const PlayersScreen = observer(props => {
const rootStore = React.useContext(MobXProviderContext).RootStore
const { players } = rootStore
const [currentPlayer, setPlayer] = useState(undefined)
const navigation = useNavigation()
const goBack = () => navigation.goBack(null)
return (
<View testID="PlayersScreen">
<PlayerList currentPlayer={currentPlayer} players={players} goBack={goBack} />
</View>
)
})
import React, { useState } from "react"
import { View } from "react-native"
import { useNavigation } from "react-navigation-hooks"
import { observer, MobXProviderContext } from "mobx-react"
import { PlayerList } from "./player-list"
export const PlayersScreen = observer(props => {
const rootStore = React.useContext(MobXProviderContext).RootStore
const { players } = rootStore
const [currentPlayer, setPlayer] = useState(undefined)
const navigation = useNavigation()
const goBack = () => navigation.goBack(null)
return (
<View testID="PlayersScreen">
<PlayerList currentPlayer={currentPlayer} players={players} goBack={goBack} />
</View>
)
})
import React, { useState } from "react"
import { View } from "react-native"
import { useNavigation } from "react-navigation-hooks"
import { observer, MobXProviderContext } from "mobx-react"
import { PlayerList } from "./player-list"
export const PlayersScreen = observer(props => {
const rootStore = React.useContext(MobXProviderContext).RootStore
const { players } = rootStore
const [currentPlayer, setPlayer] = useState(undefined)
const navigation = useNavigation()
const goBack = () => navigation.goBack(null)
return (
<View testID="PlayersScreen">
<PlayerList currentPlayer={currentPlayer} players={players} goBack={goBack} />
</View>
)
})
import React, { useState } from "react"
import { View } from "react-native"
import { useNavigation } from "react-navigation-hooks"
import { observer, MobXProviderContext } from "mobx-react"
import { PlayerList } from "./player-list"
export const PlayersScreen = observer(props => {
const rootStore = React.useContext(MobXProviderContext).RootStore
const { players } = rootStore
const [currentPlayer, setPlayer] = useState(undefined)
const navigation = useNavigation()
const goBack = () => navigation.goBack(null)
return (
<View testID="PlayersScreen">
<PlayerList currentPlayer={currentPlayer} players={players} goBack={goBack} />
</View>
)
})
// Display a "warmer" to ask for a permission type
warm: flow(function* (permissionType) {
self.permissionType = permissionType
// check if already permitted and succeed if so
let permission = yield self.rootStore.check(permissionType)
if (permission === "authorized") return true
// otherwise show warmer
self.navigateTo("warmer")
// wait for the warmer to dismiss
yield when(() => !self.isWarmerActive)
// now check permissions again
permission = yield self.rootStore.check(permissionType)
return permission === "authorized"
})
* Subject to change!
import React, { useState } from "react"
import { AppRegistry } from "react-native"
import { MainStackNavigator } from "./navigation/main-stack-navigator"
import { AppStore } from "./models/app-store"
import { NavX } from "react-navx"
function App(props) {
const [appStore] = useState(AppStore.create({}))
return (
<NavX
stores={{ appStore }}
screen={MainStackNavigator}
/>
)
}
AppRegistry.registerComponent("MyApp", () => App)
import React, { useState } from "react"
import { View } from "react-native"
import { useStore, useNavigation, observer } from "react-navx"
import { PlayerList } from "./player-list"
export const PlayersScreen = observer(props => {
const { players } = useStore("AppStore")
const [currentPlayer, setPlayer] = useState(undefined)
const navigation = useNavigation()
const goBack = () => navigation.goBack(null)
return (...)
})