Taro + Mobx 支付宝小程序数组中自定义组件渲染异常

本贴最后更新于 1820 天前,其中的信息可能已经斗转星移

2020-03-19

问题

使用 Taro + Mobx,对数组进行增删改后,在数组中使用子组件,界面没有按照数组结果进行渲染。

imageyaj6OWF.png

问题代码

src/app.tsx

import Taro, { Component, Config } from '@tarojs/taro'; import {listStore} from "./store/list"; import {Provider} from "@tarojs/mobx"; import Index from './pages/list'; export const store = { list: listStore, }; class App extends Component { config: Config = { pages: [ 'pages/list', ], }; componentDidMount() {} componentDidCatchError() {} render() { return ( <Provider store={store}> <Index /> </Provider> ); } } Taro.render(<App />, document.getElementById('app'));

src/store/list.ts

import {action, observable} from "mobx"; import { toJS } from 'mobx'; let count = 0; export class ListStore { @observable list: string[] = []; @action.bound addTestItem() { const newList = toJS(this.list).slice(0); newList.unshift(String(count)); count++; this.list = newList; } @action.bound deleteItem() { const newList = toJS(this.list).slice(0); newList.shift(); this.list = newList; } } export const listStore = new ListStore();

src/pages/list.tsx

import {Button, View} from "@tarojs/components"; import {toJS} from "mobx"; import {ListStore} from "../store/list"; import {inject, observer} from "@tarojs/mobx"; import Component = Taro.Component; import Item from "../components/Item" import {ComponentType} from "react"; interface IProps { list: ListStore; } @inject('list') @observer class List extends Component<IProps> { add = async () => { const {list} = this.props; list.addTestItem(); }; delete = async () => { const {list} = this.props; list.deleteItem(); }; render() { const list = toJS(this.props.list.list) || []; return ( <View> <Button onClick={this.add}>add</Button> <Button onClick={this.delete}>delete</Button> {list.map(count => ( <Item key={count} number={count}/> ))} </View> ); } } export default List as ComponentType;

src/components/Item.tsx

import Taro, { Component} from '@tarojs/taro'; import {observer} from "@tarojs/mobx"; import {View} from "@tarojs/components"; interface IProps { number: string; } @observer class Item extends Component<IProps> { render() { const { number } = this.props; return <View>{`number: ${number}`}</View>; } } export default Item;

解决

src/pages/list.tsx 子组件外添加标签 <View> 后可正常渲染

<View> <Item key={count} number={count}/> </View>

imagejRiEZ3T.png

返回总目录

每天 30 秒系列之小八哥

  • 30Seconds

    📙 前端知识精选集,包含 HTML、CSS、JavaScript、React、Node、安全等方面,每天仅需 30 秒。

    • 精选常见面试题,帮助您准备下一次面试
    • 精选常见交互,帮助您拥有简洁酷炫的站点
    • 精选有用的 React 片段,帮助你获取最佳实践
    • 精选常见代码集,帮助您提高打码效率
    • 整理前端界的最新资讯,邀您一同探索新世界
    488 引用 • 384 回帖 • 3 关注
  • JavaScript

    JavaScript 一种动态类型、弱类型、基于原型的直译式脚本语言,内置支持类型。它的解释器被称为 JavaScript 引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在 HTML 网页上使用,用来给 HTML 网页增加动态功能。

    729 引用 • 1275 回帖 • 2 关注
  • Bug

    Bug 本意是指臭虫、缺陷、损坏、犯贫、窃听器、小虫等。现在人们把在程序中一些缺陷或问题统称为 bug(漏洞)。

    76 引用 • 1741 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...
Vanessa
我们终此一生,就是要摆脱他人的期待,找到真正的自己。 曲靖