Taro+TypeScript - Mobx实践

前言

    在使用Mobx中,可以使用componentWillReact(生命周期钩子),当组件计划re-render (数据发生变换,页面进行渲染的时候)时会触发.

使用observable、@observable (Taro中官网实例化)

    1、在store下新建counter.ts

import { observable } from 'mobx'

// 创建的是实例对象
const counter = observable({
  counter: 0,
  counterStore() {
    this.counter++
  },
  increment() {
    this.counter++
  },
  decrement() {
    this.counter--
  },
  incrementAsync() {
    setTimeout(() => {
      this.counter++
    }, 1000)
  }
})
export default counter

    在store下新建price.ts

import { observable, action } from 'mobx'

export default class Price {
  @observable price: number = 0

  @action.bound setPrice (price: number) {
    this.price += price
  }
}

    2、在app.tsx中导入

import counter from './store/counter'
// 声明store
const store = {
  counter,
  price: new Price() // 注意此处生成实例
}
class App extends Component {render () {
    return (
      <Provider store={store}>
        <Index />
      </Provider>
    )
  }
}

    3、使用

import { observer, inject } from '@tarojs/mobx'

type propsType = {
  price: {
    price: number,
    setPrice: Function
  },
  counter: {
    counter: number,
    increment: Function,
    decrement: Function,
    incrementAsync: Function
  }
}

type stateType = {
}

interface Index {
  props: propsType,
  state: stateType
}


@inject('count', 'counter')
@observer
class Index extends Component {
    constructor(props) {
        super(props)
            this.state = {
        }
    }  
  // mobx数据改变进行渲染函数()
  componentWillReact () {}
  increment = () => {
    const { counter } = this.props
    counter.increment()
  }

  decrement = () => {
    const { counter } = this.props
    counter.decrement()
  }

  incrementAsync = () => {
    const { counter } = this.props
    counter.incrementAsync()
  }
  setPrice () {
    const { price: { setPrice } } = this.props
    setPrice(1)
  }
  
  render () {
    const {counter: { counter }, price: { price } } = this.props
    return (
      <View className='index'>
        <Button onClick={this.increment}>+</Button>
        <Button onClick={this.decrement}>-</Button>
        <Button onClick={this.incrementAsync}>Add Async</Button>
        <Text>{counter}</Text>
        <Button onClick={this.setPrice}>点击</Button>
        <Text>现在价格为:{price}</Text>
      </View>
    )
  }
}

工程化实例

    在store下新建counter.ts

import { observable, action } from 'mobx'

export default class Counter {
  @observable counter:number =  0
  @action.bound counterStore() {
    this.counter++
  }
  @action.bound increment() {
    this.counter++
  }
  @action.bound decrement() {
  this.counter--
}
  @action.bound incrementAsync() {
    setTimeout(() => {
      this.counter++
    }, 1000)
  }
}

    新建price.ts

import { observable, action } from 'mobx'

export default class Price {
  @observable price: number = 0

  @action.bound setPrice (price: number) {
    this.price += price
  }
}

    新建index.ts

import Counter from './counter'
import Price from './price'

export default {
  Counter: new Counter(),
  Price: new Price()
}

    在app.tsx使用

import store from './store'
class App extends Component {render () {
    return (
      <Provider store={store}>
        <Index />
      </Provider>
    )
  }
}

     在pages下新建mobx

import { ComponentType } from 'react'
import Taro, { Component, Config } from '@tarojs/taro'
import { View, Button, Text } from '@tarojs/components'
import { observer, inject } from '@tarojs/mobx'

type propsType = {
  price: {
    price: number,
    setPrice: Function
  },
  counter: {
    counter: number,
    increment: Function,
    decrement: Function,
    incrementAsync: Function
  }
}

type stateType = {
}

interface Index {
  props: propsType,
  state: stateType
}


@inject('price', 'counter')
@observer
class Index extends Component {
  constructor(props) {
    super(props)
    this.state = {
    }
  }
  config: Config = {
    navigationBarTitleText: 'mobx'
  }  
  // mobx数据改变进行渲染函数()
  componentWillReact () {}
  
  increment = () => {
    const { counter } = this.props
    counter.increment()
  }

  decrement = () => {
    const { counter } = this.props
    counter.decrement()
  }

  incrementAsync = () => {
    const { counter } = this.props
    counter.incrementAsync()
  }
  setPrice() {
    const { price: { setPrice } } = this.props
    setPrice(1)
  }

  render() {
    const { counter: { counter }, price: { price } } = this.props
    return (
      <View className='index'>
        <Button onClick={this.increment}>+</Button>
        <Button onClick={this.decrement}>-</Button>
        <Button onClick={this.incrementAsync}>Add Async</Button>
        <Text>{counter}</Text>
        <Button onClick={this.setPrice}>点击</Button>
        <Text>现在价格为:{price}</Text>
      </View>
    )
  }
}

export default Index as ComponentType
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: taro-plugin-tailwind是一个用于Taro开发框架的插件,它结合了Tailwind CSS和Taro框架的功能,旨在提供更便捷、高效的开发体验。 Tailwind CSS是一个功能强大、可定制的CSS框架,它通过提供一系列预定义的样类,使开发者能够快速构建出丰富多样的UI界面。而Taro是一款跨平台的前端开发框架,它可以同时构建小程序、H5和React Native等多个平台的应用。 taro-plugin-tailwind的作用就是将这两者有机地结合在一起,让开发者在使用Taro开发项目时能够直接使用Tailwind CSS提供的各种样类,从而节省编写繁琐CSS样的时间。 使用taro-plugin-tailwind,我们只需要在Taro项目中安装并配置好该插件,然后就可以在项目中使用Tailwind CSS的样类来定义组件的样。例如,我们可以使用类似"bg-blue-500"、"text-white"这样的样类来设置组件的背景颜色和文字颜色。 此外,taro-plugin-tailwind还提供了一些自定义配置的选项,允许开发者根据具体项目的需求进行样配置和扩展。这使得我们可以根据项目需要添加自定义的样类或修改现有的样类,进一步满足项目的设计需求。 总之,taro-plugin-tailwind是一个帮助开发者将Tailwind CSS快速应用到Taro项目中的插件,它简化了样开发流程,提高了开发效率,让我们可以更专注于项目的逻辑实现。 ### 回答2: taro-plugin-tailwind是一个适用于Taro前端开发框架的插件,它允许开发者在Taro项目中使用Tailwind CSS来简化样开发流程。 Tailwind CSS是一个功能强大的工具集,它提供了一系列的CSS类名,开发者可以通过组合这些类名来构建出复杂的样。使用Tailwind CSS能够大大提高开发效率,减少样代码量,并且能够保持代码的可读性和可维护性。 taro-plugin-tailwind的使用非常简单,只需要在Taro项目中安装该插件,然后在配置文件中进行相应的配置即可。该插件会自动将Tailwind CSS的类名转换为对应的样。 通过使用taro-plugin-tailwind,开发者可以在Taro项目中使用大量的预定义样类名,如颜色、背景、边框、文本样等,可以轻松实现样的定制化和重用。同时,该插件还支持自定义配置,开发者可以根据项目需求进行个性化的设置。 总之,taro-plugin-tailwind是一个非常实用的插件,它使得在Taro项目中使用Tailwind CSS更加方便和高效,能够帮助开发者快速构建出漂亮而且响应的界面。无论是新项目还是现有项目,都可以考虑使用taro-plugin-tailwind来加速开发进程。 ### 回答3: taro-plugin-tailwind是一个为Taro框架提供的Tailwind CSS的插件。Taro是一个跨平台的前端开发框架,可以使用一套代码开发多个平台的应用程序,包括小程序、H5、React Native等。 Tailwind CSS是一个高度可定制的CSS框架,它通过将原子类(例如m-4、p-2)组合来构建样,可以快速而灵活地创建各种各样的界面。Taro-plugin-tailwind的目的就是将Tailwind CSS集成到Taro框架中,提供更强大的样处理能力。 使用taro-plugin-tailwind可以让开发者更轻松地在Taro应用中使用Tailwind CSS,省去了手动配置的繁琐过程。只需要在Taro项目中安装这个插件,并在项目的配置文件中进行相应的配置,即可开始使用Tailwind CSS样。 通过taro-plugin-tailwind,开发者可以使用Tailwind CSS的所有特性,如响应布局、自定义颜色、间距、阴影等。可以通过直接引用Tailwind CSS的类名,或者使用插件提供的自定义组件,来实现所需的样效果。 总之,taro-plugin-tailwind是一个集成了Tailwind CSS的Taro插件,通过使用这个插件,开发者可以更方便地在Taro应用中使用Tailwind CSS,快速构建出具有各种样效果的界面。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值