React-Native 错误将使用 Expo 的视频添加到背景的 App.js
React-Native error adding a video using Expo to the App.js for background
这是错误信息:
错误:
不变违规:元素类型无效:应为字符串
(为了
内置组件)或 class/function(对于复合
组件)
但得到:对象。
Check the render method of `App`.
This error is located at:
in RCTView (at View.js:60)
in View (at App.js:31)
in RCTView (at View.js:60)
in View (at App.js:30)
我在我的 App.js 中添加了一个关于世博会的视频,但我收到了这个错误并且似乎无法解决。我正在尝试将背景设置为已保存到资产文件夹和桌面的视频。
App.js
import React from 'react';
import { StyleSheet, Text, View, Dimensions } from 'react-native';
import Video from 'expo';
import { MaterialIcons, Octicons } from '@expo/vector-icons';
export default class App extends React.Component {
state = {
mute: false,
fullScreen: false,
shouldPlay: true,
}
handlePlayAndPause = () => {
this.setState(prevState => ({
shouldPlay: !prevState.shouldPlay
}));
}
handleVolume = () => {
this.setState(prevState => ({
mute: !prevState.mute,
}));
}
render() {
const { width } = Dimensions.get('window');
return (
<View style={styles.container}>
<View>
<Text style={{ textAlign: 'center' }}> React Native Video </Text>
<Video
source={{ uri: './assets/background.mp4' }}
shouldPlay={this.state.shouldPlay}
resizeMode="cover"
style={{ width, height: 300 }}
isMuted={this.state.mute}
/>
<View style={styles.controlBar}>
<MaterialIcons
name={this.state.mute ? "volume-mute" : "volume-up"}
size={45}
color="white"
onPress={this.handleVolume}
/>
<MaterialIcons
name={this.state.shouldPlay ? "pause" : "play-arrow"}
size={45}
color="white"
onPress={this.handlePlayAndPause}
/>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
controlBar: {
position: 'absolute',
bottom: 0,
left: 0,
right: 0,
height: 45,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: "rgba(0, 0, 0, 0.5)",
}
});
package.json
{
"name": "empty-project-template",
"main": "node_modules/expo/AppEntry.js",
"private": true,
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"postinstall": "react-native link",
"eject": "expo eject"
},
"dependencies": {
"@babel/preset-react": "^7.0.0",
"@expo/vector-icons": "^6.3.1",
"expo": "^30.0.1",
"link": "^0.1.5",
"react": "16.3.1",
"react-native": "^0.55.0",
"react-native-vector-icons": "^5.0.0",
"react-native-video": "^3.2.1"
}
}
如果您使用的是项目目录中的视频,则必须 require
在源代码中 属性
<Video
source={require('./assets/background.mp4')}
shouldPlay={this.state.shouldPlay}
resizeMode="cover"
style={{ width, height: 300 }}
isMuted={this.state.mute}
/>
这是使用视频组件的文档https://github.com/expo/expo-docs/blob/master/versions/v25.0.0/sdk/video.md
你也应该像这样导入视频
import {Video} from 'expo'
这是错误信息:
错误: 不变违规:元素类型无效:应为字符串 (为了 内置组件)或 class/function(对于复合 组件) 但得到:对象。
Check the render method of `App`.
This error is located at:
in RCTView (at View.js:60)
in View (at App.js:31)
in RCTView (at View.js:60)
in View (at App.js:30)
我在我的 App.js 中添加了一个关于世博会的视频,但我收到了这个错误并且似乎无法解决。我正在尝试将背景设置为已保存到资产文件夹和桌面的视频。
App.js
import React from 'react';
import { StyleSheet, Text, View, Dimensions } from 'react-native';
import Video from 'expo';
import { MaterialIcons, Octicons } from '@expo/vector-icons';
export default class App extends React.Component {
state = {
mute: false,
fullScreen: false,
shouldPlay: true,
}
handlePlayAndPause = () => {
this.setState(prevState => ({
shouldPlay: !prevState.shouldPlay
}));
}
handleVolume = () => {
this.setState(prevState => ({
mute: !prevState.mute,
}));
}
render() {
const { width } = Dimensions.get('window');
return (
<View style={styles.container}>
<View>
<Text style={{ textAlign: 'center' }}> React Native Video </Text>
<Video
source={{ uri: './assets/background.mp4' }}
shouldPlay={this.state.shouldPlay}
resizeMode="cover"
style={{ width, height: 300 }}
isMuted={this.state.mute}
/>
<View style={styles.controlBar}>
<MaterialIcons
name={this.state.mute ? "volume-mute" : "volume-up"}
size={45}
color="white"
onPress={this.handleVolume}
/>
<MaterialIcons
name={this.state.shouldPlay ? "pause" : "play-arrow"}
size={45}
color="white"
onPress={this.handlePlayAndPause}
/>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
controlBar: {
position: 'absolute',
bottom: 0,
left: 0,
right: 0,
height: 45,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: "rgba(0, 0, 0, 0.5)",
}
});
package.json
{
"name": "empty-project-template",
"main": "node_modules/expo/AppEntry.js",
"private": true,
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"postinstall": "react-native link",
"eject": "expo eject"
},
"dependencies": {
"@babel/preset-react": "^7.0.0",
"@expo/vector-icons": "^6.3.1",
"expo": "^30.0.1",
"link": "^0.1.5",
"react": "16.3.1",
"react-native": "^0.55.0",
"react-native-vector-icons": "^5.0.0",
"react-native-video": "^3.2.1"
}
}
如果您使用的是项目目录中的视频,则必须 require
在源代码中 属性
<Video
source={require('./assets/background.mp4')}
shouldPlay={this.state.shouldPlay}
resizeMode="cover"
style={{ width, height: 300 }}
isMuted={this.state.mute}
/>
这是使用视频组件的文档https://github.com/expo/expo-docs/blob/master/versions/v25.0.0/sdk/video.md
你也应该像这样导入视频
import {Video} from 'expo'