【发布时间】:2020-03-01 11:40:57
【问题描述】:
我是 React 世界的新手
我有 2 个屏幕:股票和条形码。 在库存中,我导航到条形码的屏幕。 当我扫描条形码时,我会返回上一个屏幕我想用条形码设置输入文本并调用一个函数。在我的示例中 joinData();
问题是设置输入文本并调用函数。 我尝试了示例和答案,但我找不到或不明白该怎么做。
我在 componentDidUpdate() 中尝试了一些东西,但它失败了 不变违规:超过最大更新深度
Stock.js
import React, {useState} from "react";
import { ScrollView, TouchableWithoutFeedback, Dimensions, StyleSheet, FlatList, View, Alert, TouchableOpacity, TextInput } from 'react-native';
//galio
import { Block, Text, theme } from "galio-framework";
import { Button, Icon, Input } from "../components/";
export default class Stock extends React.Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
this.array = [];
this.state = {
arrayHolder: [],
Input_ITMREF: ''
};
}
// I tried this but it fails
componentDidUpdate() {
if (this.props.navigation.getParam('itmref') != 'undefined') {
this.setState({ Input_ITMREF: this.props.navigation.getParam('itmref')});
}
}
componentDidMount() {
this.setState({ arrayHolder: [...this.array] }) // Rafraîchit la liste
}
joinData = () => {
vxml = this.state.Input_ITMREF+" I do something";
}
Render() {
return (
<Block flex>
<Block row space="evenly">
<Block center>
<Input
placeholder='Code article'
onChangeText={data => this.setState({ Input_ITMREF: data })}
ref={this.myRef}
/>
</Block>
</Block>
<Block center>
<Button style={styles.button} onPress={() => this.props.navigation.navigate('Barcode')}>Barcode</Button>
<Text style={{ margin: 10 }}>Post: {this.props.navigation.getParam('itmref')}</Text>
</Block>
</Block>
);
}
}
和 Barcode.js
import React, {} from 'react';
import { ScrollView, TouchableWithoutFeedback, Dimensions, StyleSheet, FlatList, View, Alert, TouchableOpacity, TextInput } from 'react-native';
import { BarCodeScanner } from 'expo-barcode-scanner';
import { Button } from "../components/";
export default class Barcode extends React.Component {
static navigationOptions = {
header: null //hide the header bar
};
handleBarCodeScanned = ({ type, data }) => {
this.props.navigation.navigate("Stock", {
itmref: data
});
};
render() {
return (
<BarCodeScanner
onBarCodeScanned={this.handleBarCodeScanned}
style={styles.barcodeScanner}
/>
);
}
}
【问题讨论】:
-
有很多方法可以解决这个问题,对于一个简单的解决方案,将这个添加到 componentDidUpdate 条件 "this.sttate.Input_ITMREF !== this.props.navigation.getParam('itmref')"
-
"this.props.navigation.getParam('itmref')" in componentDidUpdate 返回未定义。
-
看看来自stackoverflow.com/questions/44223727/… 的gihanchanuka 的回答。他举了一个简单的例子。了解后你会很开心。