react native javascript mobx template
1.0.0
これは、JavaScriptとMobxを使用してReact-Nativeアプリを構築するための不可欠な例です。
yarn installまたはnpm installreact-native ejectreact-native run-iosまたはreact-native run-androidストアを定義する:
import { observable , action , runInAction , computed , autorun } from 'mobx' ;
import ajax from '../util/ajax'
export default class AppStore {
@ observable isLoading = true
@ observable isFailure = false
@ observable searchTerm = observable . box ( "" )
@ observable deals = [ ]
@ observable currentDealId = null
constructor ( ) {
this . searchTerm . observe ( ( value ) => {
this . fetchDeals ( value . newValue )
} , true )
}
async fetchDeals ( ) {
ajax . fetchDealSearchResults ( this . searchTerm ) . then ( data => {
runInAction ( ( ) => {
this . isLoading = false
this . deals = data
} )
} )
}
@ action setSearchTerm ( searchStr ) {
this . searchTerm . set ( searchStr )
}
@ action
setCurrentDeal ( dealId ) {
this . currentDealId = dealId
}
@ action
unsetCurrentDeal ( ) {
this . currentDealId = null
}
@ computed get currentDeal ( ) {
return this . deals . find ( ( deal ) => deal . key === this . currentDealId )
}
}コンポーネントの呼び出し:
@ inject ( "appStore" ) @ observer
class App extends React . Component {
searchDeals = ( searchTerm ) => {
this . props . appStore . setSearchTerm ( searchTerm )
}
setCurrentDeal = ( dealId ) => {
this . props . appStore . setCurrentDeal ( dealId )
}
unsetCurrentDeal = ( ) => {
this . props . appStore . unsetCurrentDeal ( )
}
render ( ) {
const appStore = this . props . appStore
}
}問題がある場合は、ここで問題を作成することをheしないでください。また、電子メールで[email protected]またはLinkedInで私に連絡してください
ありがとう
参照