简体   繁体   English

如何在 React Native 中的两个屏幕之间传递动态数据?

[英]How to pass dynamic data between two screen in React native?

I have referred official documentation of react native( https://reactnavigation.org/docs/params ).In that I observed that they are passing the static data between screens.我已经参考了 react native 的官方文档( https://reactnavigation.org/docs/params )。我观察到他们正在屏幕之间传递 static 数据。 But I want to pass data taken from user.但我想传递从用户那里获取的数据。 If someone knows how to share data then please help.如果有人知道如何共享数据,请提供帮助。 I have taken help of context api also but I failed to pass the data.我也得到了上下文 api 的帮助,但我未能传递数据。 Any source or material will also be helpful.任何来源或材料也会有所帮助。

Your issue may be related to your input.您的问题可能与您的输入有关。 It seems you are not capturing your inputs into a state variable.您似乎没有将输入捕获到 state 变量中。

Check this example from ReactNative input: https://reactnative.dev/docs/0.65/textinput从 ReactNative 输入检查此示例: https://reactnative.dev/docs/0.65/textinput

import React from "react";
import { SafeAreaView, StyleSheet, TextInput } from "react-native";

const UselessTextInput = () => {
  const [text, onChangeText] = React.useState("Useless Text");

  return (
    <SafeAreaView>
      <TextInput
        style={styles.input}
        onChangeText={onChangeText}
        value={text}
      />
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  input: {
    height: 40,
    margin: 12,
    borderWidth: 1,
    padding: 10,
  },
});

export default UselessTextInput;

Then you could use navigate:然后你可以使用导航:

navigation.navigate('Details', {
  param1: text,
});

In the other screen you could read the param1 like this:在另一个屏幕中,您可以像这样读取 param1:

route.params.param1

Checkout this code snippet.签出此代码段。

  React.useEffect(() => {
    if (route.params?.post) {
      // Post updated, do something with `route.params.post`
      // For example, send the post to the server
    }
  }, [route.params?.post]);

  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Button
        title="Create post"
        onPress={() => navigation.navigate('CreatePost')}
      />
      <Text style={{ margin: 10 }}>Post: {route.params?.post}</Text>
    </View>
  );
}

function CreatePostScreen({ navigation, route }) {
  const [postText, setPostText] = React.useState('');

  return (
    <>
      <TextInput
        multiline
        placeholder="What's on your mind?"
        style={{ height: 200, padding: 10, backgroundColor: 'white' }}
        value={postText}
        onChangeText={setPostText}
      />
      <Button
        title="Done"
        onPress={() => {
          // Pass and merge params back to home screen
          navigation.navigate({
            name: 'Home',
            params: { post: postText },
            merge: true,
          });
        }}
      />
    </>
  );
}

to pass a param to a screen将参数传递到屏幕

navigation.navigate('screenName', {
   paramName: 'valueYouwantToPass',
});

because you mentioned context API, try因为您提到了上下文 API,请尝试

Async Storage异步存储

import {AsyncStorage} from 'react-native';

or或者

Device Event Emitter设备事件发射器

import { DeviceEventEmitter} from 'react-native';

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM