如何将我的道具添加到React-Native应用中的堆栈屏幕

发布于 2025-02-07 04:50:54 字数 2965 浏览 2 评论 0原文

我正在React-Native构建一个待办事项清单应用程序。我是学习它的初学者。我在app.js文件中有一个函数名称handlesubmit() data 。我想在各个组件/屏幕中将它们作为道具传递。

这是我的尝试:

app.js

import React, { useState } from "react";
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import Welcome from './Screens/Welcome';
import Tasklist from './Screens/Tasklist';
import Addtask from './Screens/Addtask';

const Stack = createNativeStackNavigator();


export default function App() {

  const [data,setData] = useState([]);

  const handleSubmit = (task) => {
    console.log("submit pressed")
    setData((prevtodo)=> {
      return [
        {
          task:task,
          key: Math.random().toString(),
        },
        ...prevtodo
      ];
    });
  }
  
  return (
    
    <NavigationContainer>
      <Stack.Navigator screenOptions={{header: ()=>null}}>
        <Stack.Screen name='Welcome' component={Welcome}></Stack.Screen>
        <Stack.Screen name='Tasklist' component={Tasklist} data={data}></Stack.Screen>
        <Stack.Screen name='Addtask' component={Addtask} handleSubmit={handleSubmit}></Stack.Screen>
      </Stack.Navigator>
    </NavigationContainer>
  );
}

,在组件addtask中,我在

import React, { useState } from "react";
import { StyleSheet, Text, View, TextInput, Button } from "react-native";
import { StatusBar } from "expo-status-bar";

const Addtask = ({navigation, handleSubmit}) => {
 
  console.log(handleSubmit)

  const [task, setTask] = useState("");

  return (
    <View style={styles.container}>
      <StatusBar style="light" backgroundColor="midnightblue" />
      <View>
        <Text style={styles.text}>Add Task Here</Text>
      </View>

      <View>
        <TextInput
          style={styles.input}
          onChangeText={setTask}
          value={task}
          placeholder="Type your task"
          keyboardType="ascii-capable"
        />
      </View>

      <View style={styles.buttoms}>
        <View style={{margin:4}}>
          <Button color={'red'} onPress={()=>{navigation.goBack()}} title="Cancel"></Button>
        </View>
        <View style={{margin:4}}>
          <Button color={'lightblue'} onPress={()=>setTask('')} title="Clear"></Button>
        </View>
        <View style={{margin:4}}>
          <Button color={'green'} onPress={handleSubmit} title="Save"></Button>
        </View>
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  .
  .
  .
  .

});
export default Addtask;

中使用handleSubmit()在登录> addtask.js,我得到了不确定的。我该如何实现上述问题?

I am building a to-do list app in react-native. I am a beginner to learn it. I had a function name handleSubmit() and a state data in the App.js file. I wanted to pass them as props in the respective components/screens.

Here's my try:

App.js

import React, { useState } from "react";
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import Welcome from './Screens/Welcome';
import Tasklist from './Screens/Tasklist';
import Addtask from './Screens/Addtask';

const Stack = createNativeStackNavigator();


export default function App() {

  const [data,setData] = useState([]);

  const handleSubmit = (task) => {
    console.log("submit pressed")
    setData((prevtodo)=> {
      return [
        {
          task:task,
          key: Math.random().toString(),
        },
        ...prevtodo
      ];
    });
  }
  
  return (
    
    <NavigationContainer>
      <Stack.Navigator screenOptions={{header: ()=>null}}>
        <Stack.Screen name='Welcome' component={Welcome}></Stack.Screen>
        <Stack.Screen name='Tasklist' component={Tasklist} data={data}></Stack.Screen>
        <Stack.Screen name='Addtask' component={Addtask} handleSubmit={handleSubmit}></Stack.Screen>
      </Stack.Navigator>
    </NavigationContainer>
  );
}

And in the component Addtask , I am using like this

import React, { useState } from "react";
import { StyleSheet, Text, View, TextInput, Button } from "react-native";
import { StatusBar } from "expo-status-bar";

const Addtask = ({navigation, handleSubmit}) => {
 
  console.log(handleSubmit)

  const [task, setTask] = useState("");

  return (
    <View style={styles.container}>
      <StatusBar style="light" backgroundColor="midnightblue" />
      <View>
        <Text style={styles.text}>Add Task Here</Text>
      </View>

      <View>
        <TextInput
          style={styles.input}
          onChangeText={setTask}
          value={task}
          placeholder="Type your task"
          keyboardType="ascii-capable"
        />
      </View>

      <View style={styles.buttoms}>
        <View style={{margin:4}}>
          <Button color={'red'} onPress={()=>{navigation.goBack()}} title="Cancel"></Button>
        </View>
        <View style={{margin:4}}>
          <Button color={'lightblue'} onPress={()=>setTask('')} title="Clear"></Button>
        </View>
        <View style={{margin:4}}>
          <Button color={'green'} onPress={handleSubmit} title="Save"></Button>
        </View>
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  .
  .
  .
  .

});
export default Addtask;

On logging the handleSubmit() in the Addtask.js, I am getting undefined. How can I achieve the abovementioned thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉酒的小男人 2025-02-14 04:50:54

您必须使用initialParams在这里

<Stack.Screen
  name="Details"
  component={DetailsScreen}
  initialParams={{ itemId: 42 }}
/>

您可以在此处参考文档 https:> https:> https:> https:> https: //reactnavigation.org/docs/params/#initial-params

PS通过此处传递功能可能会发出警告

You have to use initialParams here

<Stack.Screen
  name="Details"
  component={DetailsScreen}
  initialParams={{ itemId: 42 }}
/>

you can refer the docs here https://reactnavigation.org/docs/params/#initial-params

P.S. passing a function in here might give a warning

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文