在MST中为Animated.Value,setInterval,require('image.png')使用什么类型?

问题描述 投票:0回答:1

我正在使用MST的本机响应,但无法解决下一个问题在MST中为new Animated.Value(1),setInterval,require('image.png')使用什么类型?尝试了stringfrozen并没有帮助。

import {Animated} from 'react-native';
import {types} from 'mobx-state-tree';

const Worksheet = types
  .model('Worksheet', {
    image: _,
    animated: _,
    timeInterval: _, 
  })

Worksheet.create({
  image: require('image.png'),
  animated: new Animated.Value(1),
  timeInterval: null | setInterval(() => {}, 1000),  
})

需要您的帮助。谢谢!

javascript react-native mobx mobx-react mobx-state-tree
1个回答
0
投票

一个选项可能是:

const Worksheet = types
  .model('Worksheet', {
    imageFile: types.string,
    animatedValue: 1,
  ).views(self => ({
    get image() {
      return require(self.imageFile);
    },
    get animated() {
      return new Animated.Value(self.animatedValue);
    },
  }).volatile({
    timeInterval: null || setInterval(() => {}, 1000)
  })

当MST无法推理时,并非所有字段都可以是类型。这就是volatile发挥作用的地方。请注意,volatile中的字段无法序列化为快照。

我不确定您对volatile的意图是什么,所以我将值作为自己的类型,并在animated中使用了计算的getter来获取所需的实际值。也许这会激发您的想法。

© www.soinside.com 2019 - 2024. All rights reserved.