简体   繁体   English

在本机反应中将数据传递给模态

[英]Passing data to a modal in react native

The data being fetched from the api is needed for other purposes in the modal.模式中需要从 api 获取的数据用于其他目的。 How do i pass data: {currency.data.prices[index].instrument} {currency.data.prices[index].closeoutAsk} {currency.data.prices[index].closeoutBid} that is in a component to a modal that is in the same component.我如何传递数据: {currency.data.prices[index].instrument} {currency.data.prices[index].closeoutAsk} {currency.data.prices[index].closeoutBid}是一个组件到模态那是在同一个组件中。 Below is the code:下面是代码:

//HomeScreen 
    import React, {useContext, useState} from 'react'
    import { Text, View, ScrollView, TouchableOpacity, Modal, TextInput } from 'react-native'
    import {ListItem, Card, Button, Icon} from 'react-native-elements'
    //import CurrencyPair from '../../CurrencyPair'
    import {firebase} from '../../../firebase/config'
    import {CurrencyContext} from '../../../context/Context'
    import styles from '../LoginScreen/styles'
    
    function HomeScreen() {
    
        const currency = useContext(CurrencyContext);
        const [modalopen, setModalOpen] = useState(false)
    
    
    return (
            <ScrollView>
              <Modal
              visible={modalopen}
              animationType={"fade"}
              >
                <View style={styles.modal}>
                  <View>
                    <Text style={{textAlign: "center", fontWeight: "bold"}}>
                    CreateAlert
                  </Text>
                  <TouchableOpacity style={styles.button} onPress={() => setModalOpen(false)}>
                    <Text style={styles.buttonTitle}>OK</Text>
                  </TouchableOpacity>
    
                  </View>
                  
                  
                </View>
    
              </Modal>
            <Card>
                <Text style={{textAlign: "center"}}>
                    Welcome
                </Text>
                <Button title="Sign Out" type="outline" onPress ={() => firebase.auth().signOut()}/>
                <Button title="My Alerts"  onPress ={() =>navigation.navigate("AlertScreen") }/>
                
            </Card>
    
            <View>
                {currency.data.prices && currency.data.prices.map((prices, index) => {
                    return (
          <ListItem
            key={index}
            onPress = {() => setModalOpen(true)}
            bottomDivider>
            <ListItem.Content>
                <ListItem.Title>
                  
                {currency.data.prices[index].instrument}        {currency.data.prices[index].closeoutAsk}         {currency.data.prices[index].closeoutBid}
                </ListItem.Title>
            </ListItem.Content>
          </ListItem>     
                    )
                })
    }
            </View>
       
        </ScrollView>
    )
    }
    export default HomeScreen

//Context //语境

import React, {createContext, useState, useEffect}from 'react'
import {ActivityIndicator} from 'react-native'
import axios from '../utils/axios'

const CurrencyContext = createContext();

const CurrencyProvider =(props) => {
    const [data, setData] = useState([])
    const [isLoading, setIsloading] = useState(true)

    useEffect(() => {
        const interval = setInterval(() => {
            const fetchpairs = async() => {
                const results = await axios.get('/v3/accounts/101-004-14328428-002/pricing?instruments=AUD_CAD%2CAUD_CHF%2CAUD_JPY%2CAUD_NZD%2CAUD_USD%2CCAD_CHF%2CCAD_JPY%2CCHF_JPY%2CEUR_AUD%2CEUR_CAD%2CEUR_CHF%2CEUR_GBP%2CEUR_NOK%2CEUR_NZD%2CEUR_USD%2CGBP_AUD%2CGBP_CAD%2CGBP_CHF%2CGBP_USD%2CGBP_JPY%2CNZD_CAD%2CNZD_CHF%2CNZD_JPY%2CUSD_CAD%2CUSD_JPY%2CUSD_CHF%2CUSD_ZAR%2CUSD_MXN')
                setData(results.data)
                setIsloading(false)
            }
            fetchpairs() 
        },1000)
      }, []);

      if(isLoading) {
        return (
            <ActivityIndicator size="large"/>
        )
    }else
    return (
        <CurrencyContext.Provider
        value={{
            data,
            setData,
            isLoading,
            setIsloading
        }}>
            {props.children}

        </CurrencyContext.Provider>

       
    )
}

export {CurrencyProvider, CurrencyContext}

you can create another state variable to store the clicked index.您可以创建另一个状态变量来存储单击的索引。

const [clickedIndex, setClickedIndex] = useState(0);

then use that in the onPress event.然后在 onPress 事件中使用它。

onPress = {() => {setModalOpen(true);setClickedIndex(index);}

then you can use this same index to display what you want in the modal.然后您可以使用相同的索引在模态中显示您想要的内容。

         <Modal
          visible={modalopen}
          animationType={"fade"}
          >
            <View style={styles.modal}>
              <View>
                <Text style={{textAlign: "center", fontWeight: "bold"}}>
                {currency.data.prices[clickedIndex].instrument}
              </Text>
              <TouchableOpacity style={styles.button} onPress={() => setModalOpen(false)}>
                <Text style={styles.buttonTitle}>OK</Text>
              </TouchableOpacity>
              </View>
            </View>
          </Modal>

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

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