简体   繁体   English

我应该使用哪个组件 class 或功能?

[英]Which component should I use class or functional?

I'm trying to use react-native-camera and I want to send tokens from the first page to the camera page.我正在尝试使用 react-native-camera 并且我想将令牌从第一页发送到相机页面。 how can I get a token on the camera page?如何在相机页面上获取令牌? I am not sure how to solve this problem, I tried to change the camera page to function based component but I get more errors:) so I'd be very happy if you guys help me out我不知道如何解决这个问题,我尝试将相机页面更改为基于 function 的组件,但我收到更多错误:) 所以如果你们帮助我,我会很高兴

the first page is function-based component第一个页面是基于功能的组件

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

const first = () => {
  const token = '12345678';

  return (
    <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
      {isData?(
        <TouchableOpacity
        onPress={() => {
          navigation.navigate('cameraV2', {token:token});
        }}
        style={{
          height: 50,
          width: '80%',
          backgroundColor: '#f45f00',
          alignItems: 'center',
          justifyContent: 'center',
        }}>
        <Text style={{fontWeight: 'bold', fontSize: 18}}>
          Giriş için tıklayın
        </Text>
      </TouchableOpacity>
      ):(
        <ActivityIndicator size='large' color='#0f0' />
      )}
    </View>
  );
};

export default first;

camera page is class based component相机页面是基于 class 的组件

import React, {Component} from 'react';
import {Button, Text, View} from 'react-native';
import {RNCamera} from 'react-native-camera';


class cameraV2 extends Component {
  navigation = this.props.navigation;
  constructor(props) {
    super(props);
    this.camera = null;
    this.barcodeCodes = [];

    this.state = {
      token:this.navigation.token,
      is_camera: 0,
      is_loading: 0,
      barcodes: [],
      camera: {
        type: RNCamera.Constants.Type.back,
        flashMode: RNCamera.Constants.FlashMode.auto,
      },
    };
  }

  barcodeRecognized = ({barcodes}) => {
    barcodes.forEach(barcode => {
        if(barcode.type!=='UNKNOWN_FORMAT'){
          console.log(barcode.data)
            this.setState({barcodes});
        }
    });
  };

  render() {
    return (
      <View style={styles.container}>
        <RNCamera
          ref={ref => {
            this.camera = ref;
          }}
          defaultTouchToFocus
          flashMode={this.state.camera.flashMode}
          mirrorImage={false}
          //onBarCodeRead={this.onBarCodeRead.bind(this)}
          onGoogleVisionBarcodesDetected={this.barcodeRecognized}
          onFocusChanged={() => {}}
          onZoomChanged={() => {}}
          permissionDialogTitle={'Permission to use camera'}
          permissionDialogMessage={
            'We need your permission to use your camera phone'
          }
          style={styles.preview}
          type={this.state.camera.type}
        />
        <View style={[styles.overlay, styles.topOverlay]}>
          <Text style={styles.scanScreenMessage}>Please scan the barcode.</Text>
          <Text style={styles.scanScreenMessage}>token: {this.state.token} </Text>
        </View>
        <View style={[styles.overlay, styles.bottomOverlay]}>
          <Button
            onPress={() =>
              this.navigation.navigate('second', {
                barcode: this.state.barcodes,
                token: token,
              })
            }
            style={styles.enterBarcodeManualButton}
            title="Enter Barcode"
          />
        </View>
      </View>
    );
  }
}

const styles = {
  container: {
    flex: 1,
  },
  preview: {
    flex: 1,
    justifyContent: 'flex-end',
    alignItems: 'center',
  },
  overlay: {
    position: 'absolute',
    padding: 16,
    right: 0,
    left: 0,
    alignItems: 'center',
  },
  topOverlay: {
    top: 0,
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
  },
  bottomOverlay: {
    bottom: 0,
    backgroundColor: 'rgba(0,0,0,0.4)',
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
  },
  enterBarcodeManualButton: {
    padding: 15,
    backgroundColor: 'white',
    borderRadius: 40,
  },
  scanScreenMessage: {
    fontSize: 14,
    color: 'white',
    textAlign: 'center',
    alignItems: 'center',
    justifyContent: 'center',
  },
};

export default cameraV2;

You just need to initialise the state with token value from route params obtained from props instead of navigation您只需要使用route params obtained from props中的令牌值初始化 state

this.state = {
      token: this.props.route.params.token,
      is_camera: 0,
      is_loading: 0,
      barcodes: [],
      camera: {
        type: RNCamera.Constants.Type.back,
        flashMode: RNCamera.Constants.FlashMode.auto,
      },
    };
  }

PS You do not need to convert the entire component to functional component just for using params PS您不需要将整个组件转换为功能组件只是为了使用参数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在同一个文件中同时使用 class 和功能组件 - how can i use both class and functional component in same file 将class组件改成功能组件:使用方法 - Changing class component to functional component : use method 我有一个 class 组件,但我得到这个 `createAnimatedComponent` 不支持无状态功能组件; 改用 class 组件 - I have a class Component but I get this `createAnimatedComponent` does not support stateless functional components; use a class component instead 如何在我的 class 组件中检测在功能组件中单击了哪个输入值? - How do I detect in my class component which input value's clicked in functional component? 我应该在我的 jQuery 中使用哪个类? - Which class should I use in my jQuery? React 16.7具有状态挂钩,在任何情况下都可以使用功能组件而不是类组件吗? - React 16.7 has State Hook, can I use functional component instead of class component in any situation? 在创建React组件时,我应该使用功能组件,React.createClass还是扩展React.Component? - When creating React components should I use functional components, React.createClass or extends React.Component? 何时使用 class 组件以及何时使用功能组件 - When to use a class component and when to use a functional component 我如何使用上下文在React中的类和功能组件之间共享状态? - How do I use context to share state between a class and a functional component in react? 哪个选项最适合在 React 功能组件中创建新的类实例? - Which option is best for creating new class instance in React functional component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM