【问题标题】:Camera does not display, state transition issue, react-native相机不显示,状态转换问题,react-native
【发布时间】:2017-12-22 16:03:13
【问题描述】:

我一直在尝试在 touchableopacity onPress 上保存一个异步存储项,然后导航到 react-native-camera 屏幕。

问题是:相机屏幕变黑。我收到以下错误:警告:在现有状态转换期间无法更新(例如在“渲染”或其他组件的构造函数中)。渲染方法应该是 props 和 state 的纯函数;构造函数的副作用是反模式,但可以移动到“componentWillMount”。

警告点指向第 27、36 和 41 行(在 AddParameters 类中)

代码如下:

AddParameters.js

import React, { Component } from 'react';

import {
  Text,
  AsyncStorage,
  View,
  TouchableOpacity,
} from 'react-native';

class AddParameters extends Component {
  constructor() {
    super()
    this.state = {
      localIds: [
        "data1",
        "data2",
        "data3",
        "data4",
        "data5",
        "data6"
      ],
    }
  }

  renderScreen = () => {
      return (
            <TouchableOpacity onPress={this._AddParameter(this.state.localIds[0])}>
              <Text>Click Me</Text>
            </TouchableOpacity>
      );
  }

  _AddParameter = (ParameterId) => {
    const { navigate } = this.props.navigation;
    AsyncStorage.setItem("myparam", ParameterId);
    navigate("CameraScreen");
  }

  render() {
    return (
      this.renderScreen()
    );
  }
}



export default AddParameters;

CameraScreen.js

'use strict';
import React, { Component } from 'react';
import {
  AppRegistry,
  Dimensions,
  StyleSheet,
  Text,
  View,
  Image,
  AsyncStorage,
} from 'react-native';
import Camera from 'react-native-camera';

class CameraScreen extends Component {

  constructor(props) {
    super(props);
    this.state = {
      mystate: '',
    };
  }

  renderCamera = () => {
    return (
      <Camera
        ref={(cam) => {
          this.camera = cam;
        }}
        style={stylesCamera.container}
        aspect={Camera.constants.Aspect.fill}>
      </Camera>
    );
  }

  render() {
    return (
      this.renderCamera()
    );
  }
}

const stylesCamera = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "transparent",
  },
});

export default CameraScreen;

任何解释都会有所帮助。提前致谢。

【问题讨论】:

    标签: react-native camera state


    【解决方案1】:

    在您的 AddParameters 文件中尝试更改:

    <TouchableOpacity onPress={this._AddParameter(this.state.localIds[0])}>
    

    收件人:

    <TouchableOpacity onPress={() => this._AddParameter(this.state.localIds[0])}>
    

    【讨论】:

    • 成功了!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-06-15
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2021-08-26
    • 2023-03-28
    • 2020-06-20
    相关资源
    最近更新 更多