javascript - 在 React Native 中将 JSON 数据传递到下一页

标签 javascript json react-native

我是初学者,仍在学习如何做出原生 react 。

在我的 React native 应用程序中,我有 2 个屏幕。 在第一页中,我有 JSON 数据 [从实时服务器获取]; 我想将此 JSON 数据传递到下一页。

我使用 react-navigation 在页面之间导航。 我传了一个数据[手机号码]到下一页。

但我不知道如何将 JSON 数据传递到下一页!

首页代码:[包含 JSON 数据]

constructor(props) { 
    super(props) 
    this.state = {

    UserMNO: ''
    } 
  }

  UserLoginFunction = () =>{

 const { UserMNO } = this.state;
 const {firstname} = this.state;
 const {lastname} = this.state;
 const {email} = this.state;
 const {profession} =this.state;

fetch('http://demo.weybee.in/react/User_Login.php', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({

  mobileno: UserMNO,

      })

}).then((response) => response.json())
      .then((responseJson) => {
        // If server response message same as Data Matched
       if(responseJson != 'Enter valid phone number' )
        {   

            console.log(responseJson[0]);
            console.log(responseJson[1]);
            console.log(responseJson[2]);
            console.log(responseJson[3]);
            //Then open Profile activity and send user email to profile activity.
            this.refs.toast.show('Login successful', 500, () => {
            const { navigation } = this.props;

            const { UserMNO }  = this.state ;

            navigation.navigate("Profile",
              {mobileno : UserMNO},
              );
    });
        }
        else{

          Alert.alert(responseJson);
        }

      }).catch((error) => {
        console.error(error);
      });

  }

首页JSON数据的console.log

log of JSON data

第二页代码:[我需要JSON数据的地方]

     <Block flex style={styles.profileCard}>
                <Block middle style={styles.avatarContainer}>
                  <Image
                    source={{ uri: Images.ProfilePicture }}
                    style={styles.avatar}
                  />
                </Block>

                <Block flex>

                  <Block middle style={styles.nameInfo}>
                    <Text bold size={28} color="#32325D">
                        {this.props.navigation.getParam("Name")}
                    </Text>
                    <Block width={width * 0.8} style={{ marginBottom: 15 }}>
                      <Input
                        editable = {false}
                        placeholder="Email id"
                        value={this.props.navigation.getParam("EmailId")}
                        style={{marginTop:20, borderRadius:30, borderWidth:3}}
                        iconContent={
                          <Icon
                            size={16}
                            color={argonTheme.COLORS.ICON}
                            name="nav-right"
                            family="ArgonExtra"
                            style={styles.inputIcons}
                          />
                        }
                      />
                    </Block>
                      <Block width={width * 0.8} style={{ marginBottom: 15 }}>
                      <Input
                        editable = {false}
                        placeholder="Mobile Number"
                        value={this.props.navigation.getParam("mobileno")}
                        style={{borderRadius:30, borderWidth:3}}
                        iconContent={
                          <Icon
                            size={16}
                            color={argonTheme.COLORS.ICON}
                            name="nav-right"
                            family="ArgonExtra"
                            style={styles.inputIcons}
                          />
                        }
                      />
                    </Block>
                    <Block width={width * 0.8} style={{ marginBottom: 15 }}>
                      <Input
                        editable = {false}
                        placeholder="profession"
                        value={this.props.navigation.getParam("Profe")}
                        style={{borderRadius:30, borderWidth:3}}
                        iconContent={
                          <Icon
                            size={16}
                            color={argonTheme.COLORS.ICON}
                            name="nav-right"
                            family="ArgonExtra"
                            style={styles.inputIcons}
                          />
                        }
                      />
                    </Block>

                  </Block>

副屏截图

Profile page

错误

不传递任何东西

最佳答案

您可以像这样发送您的响应 JSON:

navigation.navigate(
              "Profile", 
              {mobileno: UserMNO, myJSON: responseJson} 
);

并在第二个屏幕中获取它:

const responseJson = this.props.navigation.getParam("myJSON");

关于javascript - 在 React Native 中将 JSON 数据传递到下一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56669590/

相关文章:

react-native - 在 native react 中向窗口添加 View

javascript - 在 onsubmit 事件上获取表单的 id

javascript - innerHTML 从图像标签中删除结束斜线

php - 我需要对 json_encoded 对象进行 escape_string 吗?

react-native - 安装 react-native-fbsdk 后无法找到或使用自动链接库 '..."

react-native - react 原生 Webview 进程终止

Javascript 正则表达式定界符(正斜杠的任何替代方法)?

javascript - 如何在D3中对齐文本和标签

php - $_GET 和 isset() url 响应 Json

php - 解析 - JSON - 数组的数组