Skip to content

Commit

Permalink
feat(useReactApp): make hook accept params as object instead of primi…
Browse files Browse the repository at this point in the history
…tive value

this is to easily extends in the future

BREAKING CHANGE: **useReactApp** change params to object
  • Loading branch information
nampdn committed Apr 15, 2019
1 parent 097d0c1 commit ecca7e5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
17 changes: 11 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { register, useBarnStream } from 'bara'
import App from './App'
import './index.css'
import {
mapBarnWithReact,
useReactApp,
useTouchableStream,
useTouchableOpacityStream,
useTextStream,
mapBarnWithReact,
useTouchableOpacityStream,
useTouchableStream,
useViewStream,
} from './lib'

Expand All @@ -17,7 +17,7 @@ const BaraApp = () => {
version: '1.0.0',
welcome: 'Welcome to Bara React App!',
})
useReactApp('bara-app', App)
useReactApp({ name: 'bara-app', App })
mapBarnWithReact(setState)
useViewStream()
useTouchableStream()
Expand All @@ -26,5 +26,10 @@ const BaraApp = () => {
welcomeTrigger(setState)
}

const data = register(BaraApp)
console.log(data)
const bara = register(BaraApp)

if (process.env.NODE_ENV === 'development' || __DEV__) {
if (window) {
(window as any).__BARA__ = bara
}
}
29 changes: 17 additions & 12 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,29 @@ import { AppRegistry } from 'react-native'

const BaraApp = (AppComponent: ComponentType) => () => AppComponent

// TODO trigger render function when the bara app has been initialized
const render = (name: string, AppComponent: ComponentType) => {
AppRegistry.registerComponent(name, BaraApp(AppComponent))
AppRegistry.runApplication(name, {
rootTag: document.getElementById('root'),
})
export interface UseReactAppConfig {
name: string
App: ComponentType
isNative?: boolean
rootHTML?: string
}

const bridge = (name: string, App: ComponentType) => () => {
export const useReactApp = ({
name,
App,
isNative = false,
rootHTML = 'root',
}: UseReactAppConfig) => {
useInit(() => {
render(name, App)
AppRegistry.registerComponent(name, BaraApp(App))
if (!isNative) {
AppRegistry.runApplication(name, {
rootTag: document.getElementById(rootHTML),
})
}
})
}

export const useReactApp = (name: string, App: ComponentType, rootHTML: string = 'root') => {
bridge(name, App)()
}

export * from './exports'
export * from './functions'
export * from './models'
Expand Down

0 comments on commit ecca7e5

Please sign in to comment.