[英]Getting 'TypeError: interpolate is not a function' in React-Native
I am learning react-native
currently.我目前正在学习react-native
。
I'm on this Project that uses mainly - react-navgation
, FlatList
and react-native-magnus
for UI.我在这个项目中主要使用 - react-navgation
、 FlatList
和react-native-magnus
的 UI。
It takes in an Array of Objects, and uses FlatList to return a list of Cards(from Magnus UI) for each of the Array Item.它接受一个对象数组,并使用 FlatList 为每个数组项返回一个卡片列表(来自 Magnus UI)。
The Navigaton Structure is like this → Link to Image Navigaton 结构是这样的 → Link to Image
For Better Accessibility, I'm attaching my project along, hope it helps → https://wetransfer.com/downloads/4b8af09bde680e8e17bcc05a662e4ef820210604152955/6b3f64为了更好的可访问性,我附上了我的项目,希望它有所帮助 → https://wetransfer.com/downloads/4b8af09bde680e8e17bcc05a662e4ef820210604152955/6b3f64
In my Code, I'm stuck at this error →在我的代码中,我遇到了这个错误→
TypeError: interpolate is not a function. (In 'interpolate(this.progress, {
inputRange: [PROGRESS_EPSILON, 1],
outputRange: [0, 1]
})', 'interpolate' is undefined)
This error is located at:
in NavigationContainer (at App.js:13)
in App (created by ExpoRoot)
in ExpoRoot (at renderApplication.js:45)
in RCTView (at View.js:34)
in View (at AppContainer.js:106)
in RCTView (at View.js:34)
in View (at AppContainer.js:132)
in AppContainer (at renderApplication.js:39)
Stack trace:
node_modules\react-native\Libraries\LogBox\LogBox.js:148:8 in registerError
node_modules\react-native\Libraries\LogBox\LogBox.js:59:8 in errorImpl
node_modules\react-native\Libraries\LogBox\LogBox.js:33:4 in console.error
node_modules\expo\build\environment\react-native-logs.fx.js:27:4 in error
node_modules\react-native\Libraries\Core\ExceptionsManager.js:104:6 in reportException
node_modules\react-native\Libraries\Core\ExceptionsManager.js:171:19 in handleException
node_modules\react-native\Libraries\Core\setUpErrorHandling.js:24:6 in handleError
node_modules\expo-error-recovery\build\ErrorRecovery.fx.js:12:21 in ErrorUtils.setGlobalHandler$argument_0
node_modules\regenerator-runtime\runtime.js:63:36 in tryCatch
node_modules\regenerator-runtime\runtime.js:293:29 in invoke
node_modules\regenerator-runtime\runtime.js:63:36 in tryCatch
node_modules\regenerator-runtime\runtime.js:154:27 in invoke
node_modules\regenerator-runtime\runtime.js:164:18 in PromiseImpl.resolve.then$argument_0
node_modules\react-native\node_modules\promise\setimmediate\core.js:37:13 in tryCallOne
node_modules\react-native\node_modules\promise\setimmediate\core.js:123:24 in setImmediate$argument_0
node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14 in _callTimer
node_modules\react-native\Libraries\Core\Timers\JSTimers.js:181:14 in _callImmediatesPass
node_modules\react-native\Libraries\Core\Timers\JSTimers.js:441:30 in callImmediates
node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:387:6 in __callImmediates
node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6 in __guard$argument_0
node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 in flushedQueue
[native code]:null in flushedQueue
[native code]:null in invokeCallbackAndReturnFlushedQueue
CODE App.js代码App.js
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Navigator from './routes/drawer';
//Test Screens //
// import MagnusCard from './magnus/magnusCard'
export default function App() {
return (
<Navigator /> //<<
//Test Screens //
// <MagnusCard />
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
CODE drawer.js代码抽屉.js
import React from 'react';
import { View, Text } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createDrawerNavigator } from 'react-navigation-drawer';
// stacks
import HomeStack from './homeStack';
import AboutStack from './aboutStack';
// drawer navigation options
const RootDrawerNavigator = createDrawerNavigator({
Home: {
screen: HomeStack,
},
About: {
screen: AboutStack,
},
});
export default createAppContainer(RootDrawerNavigator);
CODE homeStack.js代码homeStack.js
import React from 'react';
import { View, Text } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import Home from '../screens/homeStack/home'
import ReviewDetails from '../screens/homeStack/reviewDetails'
//Shared Components
import Header from '../shared/header';
const screens = {
Home: {
screen: Home,
navigationOptions: ({ navigation }) => {
return {
headerTitle: () => <Header title="Home" navigation={navigation} />
}
}
},
ReviewDetails: {
screen: ReviewDetails,
navigationOptions: {
title: "Review Details"
}
},
}
const HomeStack = createStackNavigator(screens);
export default HomeStack;
CODE home.js代码home.js
import { StatusBar } from 'expo-status-bar';
import React, { useState } from 'react';
import { StyleSheet, Text, View, Button, FlatList, TouchableOpacity } from 'react-native';
// Shared Components
import MagnusCard from '../../magnus/magnusCard';
export default function Home({ navigation }) {
const [reviews, setReviews] = useState([
{ title: 'Zelda, Breath of Fresh Air', rating: 5, body: 'lorem ipsum', key: '1' },
{ title: 'Gotta Catch Them All (again)', rating: 4, body: 'lorem ipsum', key: '2' },
{ title: 'Not So "Final" Fantasy', rating: 3, body: 'lorem ipsum', key: '3' },
]);
return (
<View style={styles.container}>
<View style={styles.topBox}>
<Button
onPress={() => { navigation.navigate('ReviewDetails') }}
title="Details" style={styles.btnToRevDetails} />
<Text style={styles.explainerText}>←Click here to view Review Screen</Text>
</View>
<View style={styles.listOfItems}>
<FlatList data={reviews} renderItem={({ item }) => (
<TouchableOpacity>
{/* onPress={() => navigation.navigate('ReviewDetails', item)} */}
<MagnusCard />
</TouchableOpacity>
)} />
</View>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
},
topBox: {
flexDirection: 'row',
margin: 4, padding: 10, alignItems: 'center'
},
listOfItems: { margin: 10, padding: 4 },
btnToRevDetails: {
flex: 3
},
explainerText: {
flex: 7,
fontSize: 16,
}
})
Maybe your react-navigation-drawer version is conflicting with the version of react-native-reanimated.也许您的 react-navigation-drawer 版本与 react-native-reanimated 的版本冲突。 Try to run npm i --save react-native-reanimated@1.0.0
.尝试运行npm i --save react-native-reanimated@1.0.0
。
If possible share your package.json
code.如果可能,分享您的package.json
代码。
I meet this error and fixed it by downgrading react-native-reanimate
to 2.1.0
.我遇到了这个错误并通过将react-native-reanimate
降级到2.1.0
来修复它。 try it but I think there is a bug because after deleting node_modules and installing again the issue was reproduced and solved it by uninstalling the package( react-native-reanimate
) and installing it again with 2.1.0
version尝试一下,但我认为有一个错误,因为在删除 node_modules 并再次安装后,问题被重现并通过卸载包( react-native-reanimate
)并使用2.1.0
版本再次安装来解决
Possibly it's a bug which causing this error.可能是导致此错误的错误。 You can install ( react-native-reanimated@1.13.1
) and then expo start --clear
您可以安装( react-native-reanimated@1.13.1
)然后expo start --clear
"interpolate() was renamed to interpolateNode() in Reanimated 2." “interpolate() 在 Reanimated 2 中被重命名为 interpolateNode()。”
1)Open the Drawer.js file found in /node_modules/react-navigation-drawer/lib/module/views/Drawer.js folder 1)打开 /node_modules/react-navigation-drawer/lib/module/views/Drawer.js 文件夹中的 Drawer.js 文件
I too faced this issue and broke my head for the last few weeks.在过去的几周里,我也遇到了这个问题并打破了我的头。 This looks to be the version conflict between react-navigation-drawer package with react-native-reanimated.这看起来是 react-navigation-drawer package 与 react-native-reanimated 之间的版本冲突。 Just now got it fixed.刚刚修好了。 interpolate() was renamed to interpolateNode() in react-native-reanimated@2+.在 react-native-reanimated@2+ 中将 interpolate() 重命名为 interpolateNode()。 No need to downgrade the react-native-reanimated package.无需降级 react-native-reanimated package。
Simple way to fix for now is (I tried to submit a pull request on react-navigation-drawer library, however, it is made read only. So you can change this locally)现在修复的简单方法是(我尝试在 react-navigation-drawer 库上提交拉取请求,但是,它是只读的。所以你可以在本地更改它)
Rebuild your code.重建你的代码。 It should work.它应该工作。
I also faced this issue and you don't have to downgrade your react-native-reanimated.我也遇到了这个问题,您不必降级您的 react-native-reanimated。 In the latest versions of reanimated, interpolate was changed to interpolateNode.在最新版本的 reanimated 中,interpolate 已更改为 interpolateNode。 So all you have to do is to open the Drawer.js file found in:因此,您所要做的就是打开位于以下位置的Drawer.js文件:
node_modules\react-navigation-drawer\lib\module\views and replace interpolate with interpolateNode . node_modules\react-navigation-drawer\lib\module\views并将interpolate替换为interpolateNode 。 It is in two places , so make sure you change both of them.它在两个地方,所以一定要同时改变它们。
This should fix the issue.这应该可以解决问题。
import * as React from 'react';
import { Text, View, TouchableOpacity } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs';
import Animated from 'react-native-reanimated';
import { createStackNavigator } from '@react-navigation/stack';
function MyTabBar({ state, descriptors, navigation, position }) {
console.log("hi")
return (
<View style={{ flexDirection: 'row', paddingTop: 20 }}>
{state.routes.map((route, index) => {
const { options } = descriptors[route.key];
const label =
options.tabBarLabel !== undefined
? options.tabBarLabel
: options.title !== undefined
? options.title
: route.name;
const isFocused = state.index === index;
const onPress = () => {
const event = navigation.emit({
type: 'tabPress',
target: route.key,
});
if (!isFocused && !event.defaultPrevented) {
navigation.navigate(route.name);
}
};
const onLongPress = () => {
navigation.emit({
type: 'tabLongPress',
target: route.key,
});
};
// modify inputRange for custom behavior
const inputRange = state.routes.map((_, i) => i);
const opacity = Animated.interpolateNode(position, {
inputRange,
outputRange: inputRange.map((i) => (i === index ? 1 : 0)),
});
return (
<TouchableOpacity
accessibilityRole="button"
accessibilityState={isFocused ? { selected: true } : {}}
accessibilityLabel={options.tabBarAccessibilityLabel}
testID={options.tabBarTestID}
onPress={onPress}
onLongPress={onLongPress}
style={{ flex: 1 }}>
<Animated.Text style={{ opacity }}>{label}</Animated.Text>
</TouchableOpacity>
);
})}
</View>
);
}
function HomeScreen() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Home!</Text>
</View>
);
}
function SettingsScreen() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Settings!</Text>
</View>
);
}
function ProfileScreen() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Profile!</Text>
</View>
);
}
const Stack = createMaterialTopTabNavigator();
export default function App() {
return (
<NavigationContainer>
{/* <Stack.Navigator>*/}
<Stack.Navigator tabBar={(props) => <MyTabBar {...props} />}>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Settings" component={SettingsScreen} />
<Stack.Screen name="Profile" component={ProfileScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.