【问题标题】:React Native StackNavigator Reference Null on PropsReact Native StackNavigator Reference Null on Props
【发布时间】:2020-02-24 22:57:54
【问题描述】:

我正在使用@react-navigation/stack 在我们的应用程序上使用它。 我们有一个问题,在我们导航到 Detail 组件后,我们需要导航回 Main 组件,Detail 组件有一个 Reference 用于输入以将焦点放在挂载上。

所以我们有了这个。

    const input = React.createRef();
    class Barcode extends React.Component {

       componentDidMount() {
            if(this.input){ 
                setTimeout(() => {
                    this.input.current.focus();
                }, 400);
            }
        }

  moveUpdate() {
        const { barcodeReducerMessageVisible, barcodeReducerMessage, barcodeReducerMessageFinished } = this.props;
        if(barcodeReducerMessageFinished) {
          this.props.navigation.navigate('Search');
        }
      }


    render() {
      return ( <ScrollView keyboardShouldPersistTaps={'handled'}>
                    <Input
                      label="Código de barras"
                      placeholder="Código de barras"
                      errorStyle={{ color: "red" }}
                      inputStyle={{ marginTop: 40, marginBottom: 40 }}
                      onChangeText={textBarcode => this.setState({ textBarcode })}
                      value={textBarcode}
                      ref={(ref)=>{this.input = ref}}
                      onBlur={() => this.Save()}
                    /> </ScrollView> );
    }
    } 

所以 moveUpdate 导航到“搜索”,在搜索时我们有这样的内容:

  ChangeBarCode(stockidRewn = null) {
    this.props.navigation.navigate('Barcode', { stockidRewn } ,{ stockidRewn: stockidRewn });
  }

  <ListItem
                    leftAvatar={{ source: { uri: searchProductReducerRepos[key].vtiger_productid } }}
                    key={key}
                    title={searchProductReducerRepos[key].description}
                    subtitle={description}
                    bottomDivider
                    onPress={() => this.ChangeBarCode(searchProductReducerRepos[key].stockid)}
                  />

当我再次调用 onPress 转到 Barcode 时,我得到:

TypeError: undefined is not an object (_this.input.current.focus)

不知道引用是否声明不正确。

有什么建议吗?

【问题讨论】:

    标签: react-native react-props react-ref


    【解决方案1】:

    你应该在组件内部定义 ref

    class Barcode extends React.Component {
    
        // declaring ref inside the component
        input = React.createRef();
    
        componentDidMount() {
            if (this.input) {
                setTimeout(() => {
                    this.input.current.focus();
                }, 400);
            }
        }
    
        moveUpdate() {
            const { barcodeReducerMessageVisible, barcodeReducerMessage, barcodeReducerMessageFinished } = this.props;
            if (barcodeReducerMessageFinished) {
                this.props.navigation.navigate('Search');
            }
        }
    
    
        render() {
            return (<ScrollView keyboardShouldPersistTaps={'handled'}>
                <Input
                    label="Código de barras"
                    placeholder="Código de barras"
                    errorStyle={{ color: "red" }}
                    inputStyle={{ marginTop: 40, marginBottom: 40 }}
                    onChangeText={textBarcode => this.setState({ textBarcode })}
                    value={textBarcode}
                    ref={(ref) => { this.input = ref }}
                    onBlur={() => this.Save()}
                /> </ScrollView>);
        }
    } 
    
    猜你喜欢
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多