我正在使用 React Native 和 MST,但无法解决下一个问题
在 MST 中,对于 new Animated.Value(1)
、setInterval、require('image.png') 使用什么类型?
尝试了字符串
、卡住
,但没有帮助。
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
内的字段无法序列化为快照。
我不确定您对animated
的意图是什么,因此我将值作为其自己的类型,并在views
中使用计算的getter来获取实际的东西你要。也许这会激发您的想法。
关于javascript - 在 MST 中对于 Animated.Value、setInterval、需要 ('image.png' )使用什么类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58763913/