反应本地-将导航道具从父母传递给孩子

NoIDEUh

我正在尝试创建一个简单的移动应用程序,但是我对此很陌生。我花了一些时间寻找我遇到的错误。看来这是一个常见问题,但是它们都具有相同/相似的解决方案,但这些解决方案对我不起作用。

我想做什么?现在,该应用程序有两个页面,主屏幕(“概述卡”)和“添加卡”屏幕。

  1. 总览卡上有一个按钮,可让您添加卡片。
  2. 添加卡允许您填写一些TextInput框,
  3. 添加卡允许您按保存按钮,然后返回到“概览卡”屏幕,并查看您在表格中输入的数据。

但是,我陷入了第3步。我试图使“保存”按钮将用户导航回“概览卡”,但实际上只是错误。

以下是我的代码,出现的错误以及尝试过的内容。

App.js

import React from 'react';
import { StyleSheet, Text, TextInput, View, Button, TouchableOpacity, ShadowPropTypesIOS } from 'react-native';
import AddCard from './components/AddCard.js';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { useNavigation } from '@react-navigation/native';

function HomeScreen({ navigation }) {
  return (
    <View style={styles.homeContainer}>
      <Button title="Add Card" onPress={() => navigation.navigate('Add Card')}/>
      {/* <Text value={this.props.inputValFT}/> */}
      <Text style={styles.textStyle} >VISA xxxx</Text>
      <Text style={styles.textStyle}>MASTERCARD xxxx</Text>
      <Text style={styles.textStyle}>AMEX xxxx</Text>
    </View>
  );
}

function AddCardScreen() {
  return (
    <View style={styles.addCardContainer}>
      <AddCard navigation={this.props.navigation} /> // **HERE**
    </View>
  );
}

const Stack = createStackNavigator();

function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="Home" component={HomeScreen} options={{ title: 'Overview Cards' }} />
        <Stack.Screen name="Add Card" component={AddCardScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}
// function AddCardButton(){
//       return (
//           <View style={styles.buttonContainer}>
//               <TouchableOpacity>
//                   <Text style={styles.button}>Add Card</Text>
//               </TouchableOpacity>
//           </View>
//       );
//   }

export default App;

const styles = StyleSheet.create({
  homeContainer: {
    flex: 1,
    backgroundColor: '#ef95b1',
    alignItems: 'center',
    justifyContent: 'flex-start',
  },
  addCardContainer: {
    flex: 1,
    backgroundColor: '#28cdf0',
    justifyContent: 'flex-start',
  },
  buttonContainer: {
    flexDirection: 'row',
    alignSelf: 'flex-end',
    marginTop: 15,
  },
  button: {
    flexDirection: 'row',
    alignSelf: 'flex-end',
    marginTop: 15,
    right: 10,
    backgroundColor: '#2565ae',
    borderWidth: 1,
    borderRadius: 12,
    color: 'white',
    fontSize: 15,
    fontWeight: 'bold',
    overflow: 'hidden',
    padding: 10,
    textAlign:'center',
  },
  textStyle: {
    padding: 10,
  }
});

Navigation.js

import React, { Component } from 'react';
import { View, Text } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import AddCardScreen from './AddCard';

  const RootStack = createStackNavigator(
    {
      Home: HomeScreen,
      AddCard: AddCardScreen,
    },
    {
      initialRouteName: 'Home',
    }
  );

  const AppContainer = createAppContainer(RootStack);

  const styles = StyleSheet.create({
    container: {
      flex: 1,
      backgroundColor: '#ef95b1',
      alignItems: 'center',
      justifyContent: 'flex-start',
    },
    textStyle: {
      padding: 10,
    }
  });

  export default createAppContainer(Navigation);

AddCard.js

import React, { Component } from 'react';
import { StyleSheet, View, Text, TextInput, TouchableOpacity } from 'react-native';
import { Input } from 'react-native-elements'
import { ScrollView } from 'react-native-gesture-handler';

// import { loadSettings, saveSettings } from '../storage/settingsStorage';

class AddCardScreen extends Component {
   constructor(props) {
    super(props);
    this.state = {
        firstTwo  : '',
        lastFour  : '',
        recentAmt : ''
    };

    this.addFT = this.addFT.bind(this)
    this.addLF = this.addLF.bind(this)
    this.addRecAmt = this.addRecAmt.bind(this)
   }

   static navigationOptions = {
       title: 'Add Card'
    };

   addFT(firstTwo) {
    this.setState(Object.assign({}, this.state.firstTwo, { firstTwo }));
  }

  addLF(lastFour) {
    this.setState(Object.assign({}, this.state.lastFour, { lastFour }));
  }

  addRecAmt(recentAmt) {
    this.setState(Object.assign({}, this.state.recentAmt, { recentAmt }));
  }

  // handleSubmit() {
  //  alert('New card saved. Returning to Home to view addition.');
  //  navigation.navigate('Home')
  // } // firstTwo, lastFour, recentAmt

    render() {
        const {navigation} = this.props;
        return (
            <ScrollView>
                <View style={styles.inputContainer}>
                    <Text h1> "Add a new card!" </Text> 
                    <TextInput 
                    style={styles.textInput}
                    placeholder="First two digits of card"
                    placeholderTextColor="#000000"
                    keyboardType={'number-pad'}
                    maxLength = {2}

                    onChangeText={this.addFT}
                    inputValFT={this.state.firstTwo}
                    />
                    <TextInput
                    style={styles.textInput}
                    placeholder="Last four digits of card"
                    placeholderTextColor="#000000"
                    keyboardType={'number-pad'}
                    maxLength = {4}

                    onChangeText={this.addLF}
                    inputValLF={this.state.lastFour}
                    />
                    <TextInput
                    style={styles.textInput}
                    placeholder="Most recent dollar amount"
                    placeholderTextColor="#000000"
                    keyboardType={'decimal-pad'}

                    onChangeText={this.addRecAmt}
                    inputValRA={this.state.recentAmt}
                    />
                </View>
                <View style={styles.inputContainer}>
                    <TouchableOpacity 
                    style={styles.saveButton}
                    onPress={() => navigation.navigate('Home')}> // ** HERE 2 **
                        <Text style={styles.saveButtonText}>Save</Text>
                    </TouchableOpacity>
                </View>
            </ScrollView>
        );
    }
}
// this.handleSubmit.bind(this)
export default AddCardScreen;

const styles = StyleSheet.create({
    inputContainer: {
        paddingTop: 15
      },
      textInput: {
        borderColor: '#FFFFFF',
        textAlign: 'center',
        borderTopWidth: 1,
        borderBottomWidth: 1,
        height: 50,
        fontSize: 17,
        paddingLeft: 20,
        paddingRight: 20
      },
      saveButton: {
        borderWidth: 1,
        borderColor: '#007BFF',
        backgroundColor: '#007BFF',
        padding: 15,
        margin: 5
      },
      saveButtonText: {
        color: '#FFFFFF',
        fontSize: 20,
        textAlign: 'center'
      }

});

我得到的错误:

在App.js中,您可以看到我放入的**这里。当我尝试运行它时,应用程序可以很好地加载,直到我单击“添加卡”按钮。我收到此错误:undefined is not an object (evaluating 'this.props.navigation')

如果我navigate={this.props.navigation}从App.js中删除部分,则该应用程序将按原样加载,但是这次,我可以单击“添加卡”按钮,并毫无问题地进入下一个屏幕。我填写了表单(AddCard.js中的TextInput部分),但是当我单击“保存”按钮时,应用程序崩溃了。错误是:TypeError: undefined is not an object (evaluating 'navigation.navigate')很有可能是因为我在onPress上做的事情,它在AddCard.js中说** HERE 2 **。handleSubmit()当前已被注释掉,但是它曾经在onPress内部。

我尝试过的

我看到的一些答案是,我需要将导航从父级传递到子级,这样才能使它正常工作。通过尝试,我得到了前面提到的错误。我还看到有人提到使用“ withNavigation”或“ useNavigation”,这应该允许孩子访问导航,但这对我也不起作用。以下是我尝试关注的一些链接。

如何将导航道具从父组件传递到标题?

将navigation.navigate传递给子组件

https://reactnavigation.org/docs/use-navigation/

感谢您的阅读,希望我的解释很清楚。

安德烈·奥拉(Andrei Olar)

我认为您的问题在这里:

function AddCardScreen({ navigation }) {
  return (
    <View style={styles.addCardContainer}>
      <AddCard navigation={navigation} />
    </View>
  );
}
  • 没有this,您不在类组件中,因此this不存在
  • 您尝试传递的prop应该被调用navigation,而不应该被调用navigate,因为这就是您尝试在子组件中访问它的方式。
  • navigationprop需要在函数参数内部进行解构function AddCardScreen({ navigation }),就像您已经完成的一样HomeScreen

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从父母到孩子的反应传递道具行为异常

来自分类Dev

反应导航传递导航道具

来自分类Dev

反应本地反应导航SafeArea问题

来自分类Dev

通过图像的SRC将孩子传递给父母时遇到问题。反应JS

来自分类Dev

反应redux将状态传递给孩子

来自分类Dev

在导航栏上反应本地渲染视图

来自分类Dev

反应本地导航器渲染方法错误

来自分类Dev

将道具从父母传递给孩子时出错-类型无效

来自分类Dev

对本地传递的onPress做出反应

来自分类Dev

将本地存储分配给反应状态。反应钩

来自分类Dev

反应:将道具传递给后代

来自分类Dev

反应:将道具传递给后代

来自分类Dev

将功能从父母传递给孩子

来自分类Dev

Blazor将数据从父母传递给孩子

来自分类Dev

在开发过程中如何将本地反应库与本地反应项目链接?

来自分类Dev

反应本地堆栈导航按钮onpress不起作用

来自分类Dev

反应本地传递route.params以获取请求

来自分类Dev

将部分文本作为链接。(反应-本地化)

来自分类Dev

将状态保存到本地存储中以做出反应

来自分类Dev

将本地解析 Lottie 文件反应为状态?

来自分类Dev

反应:如何将道具传递给状态组件?

来自分类Dev

反应:将道具传递给父返回<empy string>

来自分类Dev

反应,将功能作为道具传递给不同的组件

来自分类Dev

对从孩子那里获得父母道具的正确方法做出反应

来自分类Dev

无法通过带有反应导航的导航道具黑白屏幕

来自分类Dev

导航道具是一成不变的吗?-反应导航v4

来自分类Dev

反应导航,无法将标题传递给标题

来自分类Dev

如何将点击处理程序从父母正确地传递给孩子?

来自分类Dev

反应破坏状态传递给组件道具

Related 相关文章

热门标签

归档