使用 MobX 开发 React Native 应用 @observer 引用报错

import {

AppRegistry,

Navigator

} from ‘react-native’

class ReactNativeMobX extends Component {

renderScene (route, navigator) {

return <route.component {…route.passProps} navigator={navigator} />

}

configureScene (route, routeStack) {

if (route.type === ‘Modal’) {

return Navigator.SceneConfigs.FloatFromBottom

}

return Navigator.SceneConfigs.PushFromRight

}

render () {

return (

<Navigator

configureScene={this.configureScene.bind(this)}

renderScene={this.renderScene.bind(this)}

initialRoute={{

component: App,

passProps: {

store: ListStore

}

}} />

)

}

}

AppRegistry.registerComponent(‘ReactNativeMobX’, () => ReactNativeMobX)

在入口文件中我们创建了一个基本的导航状态,并导入了新创建的数组存储器。在 initialRoute 中我们传入数据存储作为属性。我们还把已经创建的组件 App 作为初始路由。App 将会访问属性中的数据存储。

在 configureScene 中,我们检查类型(type)是否是 ‘Modal’,是则返回 floatFromBottom 这个场景配置项,可以把下一个场景也设置为模态的。

现在,我们来创建应用组件。这确是一个大型的组件,还有很多需要完善的地方,但我们创建了一个允许增加和删除列表条目的基本用户界面。调用数据存储的方法来和我们的应用状态交互。app/App.js 的内容:

import React, { Component } from ‘react’

import { View, Text, TextInput, TouchableHighlight, StyleSheet } from ‘react-native’

import {observer} from ‘mobx-react/native’

import NewItem from ‘./NewItem’

@observer

class TodoList extends Component {

constructor () {

super()

this.state = {

text: ‘’,

showInput: false

}

}

toggleInput () {

this.setState({ showInput: !this.state.showInput })

}

addListItem () {

this.props.store.addListItem(this.state.text)

this.setState({

text: ‘’,

showInput: !this.state.showInput

})

}

removeListItem (item) {

this.props.store.removeListItem(item)

}

updateText (text) {

this.setState({text})

}

addItemToList (item) {

this.props.navigator.push({

component: NewItem,

type: ‘Modal’,

passProps: {

item,

store: this.props.store

}

})

}

render() {

const { showInput } = this.state

const { list } = this.props.store

return (

My List App

{!list.length ? : null}

{list.map((l, i) => {

return

<Text

style={styles.item}

onPress={this.addItemToList.bind(this, l)}>{l.name.toUpperCase()}

<Text

style={styles.deleteItem}

onPress={this.removeListItem.bind(this, l)}>Remove

})}

<TouchableHighlight

underlayColor=‘transparent’

onPress={

this.state.text === ‘’ ? this.toggleInput.bind(this)
this.addListItem.bind(this, this.state.text)

}

style={styles.button}>

{this.state.text === ‘’ && ‘+ New List’}

{this.state.text !== ‘’ && ‘+ Add New List Item’}

{showInput && <TextInput

style={styles.input}

onChangeText={(text) => this.updateText(text)} />}

);

}

}

const NoList = () => (

No List, Add List To Get Started

)

const styles = StyleSheet.create({

itemContainer: {

borderBottomWidth: 1,

borderBottomColor: ‘#ededed’,

flexDirection: ‘row’

},

item: {

color: ‘#156e9a’,

fontSize: 18,

flex: 3,

padding: 20

},

deleteItem: {

flex: 1,

padding: 20,

color: ‘#a3a3a3’,

fontWeight: ‘bold’,

marginTop: 3

},

button: {

height: 70,

justifyContent: ‘center’,

alignItems: ‘center’,

borderTopWidth: 1,

borderTopColor: ‘#156e9a’

},

buttonText: {

color: ‘#156e9a’,

fontWeight: ‘bold’

},

heading: {

height: 80,

justifyContent: ‘center’,

alignItems: ‘center’,

borderBottomWidth: 1,

borderBottomColor: ‘#156e9a’

},

headingText: {

color: ‘#156e9a’,

fontWeight: ‘bold’

},

input: {

height: 70,

backgroundColor: ‘#f2f2f2’,

padding: 20,

color: ‘#156e9a’

},

noList: {

flex: 1,

justifyContent: ‘center’,

alignItems: ‘center’

},

noListText: {

fontSize: 22,

color: ‘#156e9a’

},

})

export default TodoList

我来解释此文件中可能不明确的地方。如果你有什么还不明白的,请留言,我会更新和回复。

  1. mobx-react/native 导入 observer

  2. 使用 @observer 装饰器描述类,确保相关数组变化后组件独立地重渲染;

  3. 导入已经创建好的组件 NewItem。这是我们要增加新条目时转向的组件;

  4. addListItem中,把 this.state.text 传入 this.props.store.addListItem。在与输入框绑定的 updateText 中会更新 this.state.text

  5. removeListItem 中调用 this.props.store.removeListItem 并传入条目;

  6. addItemToList 中调用 this.props.navigator.push,传入条目和数组存储两个参数;

const { list } = this.props.store

  1. 在 render 方法中,也创建了界面,并绑定了类的方法

最后,创建 NewItem 组件:

import React, { Component } from ‘react’

import { View, Text, StyleSheet, TextInput, TouchableHighlight } from ‘react-native’

class NewItem extends Component {

constructor (props) {

super(props)

this.state = {

newItem: ‘’

}

}

addItem () {

if (this.state.newItem === ‘’) return

this.props.store.addItem(this.props.item, this.state.newItem)

this.setState({

newItem: ‘’

})

}

updateNewItem (text) {

this.setState({

newItem: text

})

}

render () {

const { item } = this.props

return (

<View style={{flex: 1}}>

{ item.name}

<Text

onPress={this.props.navigator.pop}

style={styles.closeButton}>×

{!item.items.length && }

{item.items.length ? : }

<View style={{flexDirection: ‘row’}}>

<TextInput

value={this.state.newItem}

onChangeText={(text) => this.updateNewItem(text)}

style={styles.input} />

<TouchableHighlight

onPress={this.addItem.bind(this)}

style={styles.button}>

Add

)

}

}

const NoItems = () => (

No Items, Add Items To Get Started

)

const Items = ({items}) => (

<View style={{flex: 1, paddingTop: 10}}>

{items.map((item, i) => {

return • {item}

})

}

)

const styles = StyleSheet.create({

heading: {

height: 80,

justifyContent: ‘center’,

alignItems: ‘center’,

borderBottomWidth: 1,

borderBottomColor: ‘#156e9a’

},

headingText: {

color: ‘#156e9a’,

fontWeight: ‘bold’

},

input: {

height: 70,

backgroundColor: ‘#ededed’,

padding: 20,

flex: 1

},

button: {

width: 70,

height: 70,

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
**

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-autWnNKI-1715846670000)]

[外链图片转存中…(img-7bFpniFN-1715846670002)]

[外链图片转存中…(img-8MOFGvev-1715846670003)]

[外链图片转存中…(img-cz54M4Nv-1715846670004)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值