我正在使用MST的本机响应,但无法解决下一个问题在MST中为new Animated.Value(1)
,setInterval,require('image.png')使用什么类型?尝试了string
,frozen
并没有帮助。
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),
})
需要您的帮助。谢谢!
一个选项可能是:
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来获取所需的实际值。也许这会激发您的想法。