简体   繁体   English

如何将数据从一个组件实时传递到另一个组件?

[英]How do i pass data from one component to another in realtime?

I am trying to pass data that I pull from a SQLDatabase into another component that would display it.我正在尝试将从 SQLDatabase 中提取的数据传递到另一个显示它的组件中。 I'm not sure how to do it exactly...我不确定该怎么做...

In my App.js在我的 App.js

This calls CustomList这调用了 CustomList

import CustomList from './components/FlatList';
export default function App() {
  return(
    <CustomList />
  );
};

which哪个

In my CustomList在我的自定义列表中

import Data from './Data';
...
export default function CustomList() {
    //Sets up Getter , Setter , Initial Data
    const [data, setData] = useState(Data);
    ...

    return (
       <FlatList 
            ListHeaderComponent = {header}
            data = {data}
            keyExtractor = { (item) => (item.id).toString()}
            ItemSeparatorComponent = { () => <View style={styles.itemSeparator}></View>}
            contentContainerStyle={ {borderBottomColor:'grey', borderBottomWidth: 1} }
            renderItem = { ({item, index}) => <ListItem item={item} index={index}/>}
       />
...

The CustomList above works if I import hard-coded data below如果我在下面导入硬编码数据,则上面的 CustomList 有效

In my Data.js在我的 Data.js

const Data = [
    {id: 1, text: 'Boadb'},
    {id: 2, text: 'Joe'},
    {id: 3, text: 'Jane'},
    {id: 4, text: 'John'},
    {id: 5, text: 'Janet'},
    {id: 6, text: 'Janet'},
    {id: 7, text: 'Janet'},
    {id: 8, text: 'Janet'},
];

export default Data;

However, I want a real-time syncing database that will update the CustomList whenever changes are made.但是,我想要一个实时同步数据库,只要进行更改,它就会更新 CustomList。

In my SQLData.js在我的 SQLData.js

let helperArray;
...
export default function SQLData() {
     ...
     function querySuccess(tx, results) {
         ...
         helperArray = [];
         //Go through each item in dataset
         for (let i = 0; i < len; i++) {
         let row = results.rows.item(i);
         helperArray.push(row);
      }
    ...
    return ();
  };

As you can see from the code above, I have put the data pulled from the SQLDatabase into a variable helperArray.从上面的代码可以看出,我已经把从 SQLDatabase 拉出来的数据放到了一个变量 helperArray 中。 I was wondering how do I import it similarly like 'Data.js' and have it output the same way!我想知道如何像“Data.js”一样导入它并以同样的方式让它 output ! Thanks谢谢

How do I pass data from one component to another in realtime?如何将数据从一个组件实时传递到另一个组件?


There are several ways of doing this, one of which is through props. 有几种方法可以做到这一点,其中一种是通过道具。 It technically is not real time, but it is close and probably fast enough for your use case. 从技术上讲,它不是实时的,但对于您的用例来说它很接近并且可能足够快。
https://reactjs.org/docs/components-and-props.html https://reactjs.org/docs/components-and-props.html
Good news: You are already doing this here: 好消息:您已经在这里这样做了:
 <FlatList ListHeaderComponent = {header} data = {data} keyExtractor = { (item) => (item.id).toString()} ItemSeparatorComponent = { () => <View style={styles.itemSeparator}></View>} contentContainerStyle={ {borderBottomColor:'grey', borderBottomWidth: 1} } renderItem = { ({item, index}) => <ListItem item={item} index={index}/>} />

Where you are passing data as a prop to FlatList referencing CustomList's data variable.您将数据作为道具传递给 FlatList 引用 CustomList 的数据变量。

 data = {data}

So you want helperArray's information in the CustomList component.因此,您需要在 CustomList 组件中获取 helperArray 的信息。 You could utilize React's Lifecycle to fetch the information once in your App Component's (or CustomList's) mounting phase .您可以利用 React 的 生命周期在您的 App 组件(或 CustomList 的)安装阶段获取信息一次。 The mounting phase is when an instance of a component is being created and inserted into the DOM:安装阶段是在创建组件实例并将其插入 DOM 时:
If you need to load data from a remote endpoint,componentDidMount is a good place to instantiate the network request .如果您需要从远程端点加载数据,componentDidMount 是一个实例化网络请求的好地方
Once you received the data from the DB, set it to your Component's state.从 DB 收到数据后,将其设置为组件的 state。 Then if you are loading it in the App Component change your return statement to be:然后,如果您在 App 组件中加载它,请将您的 return 语句更改为:

 return( <CustomList data = {helperArray(the reference to the state )}/> );

Another question is另一个问题是

However, I want a real-time syncing database that will update the CustomList whenever changes are made.但是,我想要一个实时同步数据库,只要进行更改,它就会更新 CustomList。

So the answer to this is very tricky and will depend on your tech stack.所以这个问题的答案非常棘手,取决于你的技术堆栈。 You could host a back-end application that acts as a REST controller for your front end.您可以为您的前端托管一个充当 REST controller 的后端应用程序。 If you are not familiar it would look like this如果你不熟悉,它看起来像这样
Front End (React WebApp) makes changes to data and clicks submit (or any other event) ->前端(React WebApp)对数据进行更改并单击提交(或任何其他事件)->
Front End will use a library(Axios.js is very popular with React) to make a HTTP request to the backend ->前端将使用一个库(Axios.js 在 React 中非常流行)向后端发出 HTTP 请求 ->
BackEnd (Spring server / Node.js server, etc.) receives that HTTP request processes it, then creates a connection with a DB (SQL, Mongo, etc.)->后端(Spring 服务器/Node.js 服务器等)接收到 HTTP 请求对其进行处理,然后创建与 DB(SQL、Mongo 等)的连接->
Back End will then use that connection to write to the database (if using Spring JPA, is using Node.js mssql) ->然后后端将使用该连接写入数据库(如果使用 Spring JPA,则使用 Node.js mssql)->
Now with your updated data in your DB, the next time someone visits your front end application it will make a request to get the data from your back end then populate the page with that data.现在有了数据库中的更新数据,下次有人访问您的前端应用程序时,它将请求从您的后端获取数据,然后用该数据填充页面。

Here is another great answer to a similar question 这是对类似问题的另一个很好的答案

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

相关问题 如何在React中将单击的组件中的数据从一条路径传递到另一条路径? - How do I pass data from a clicked component from one Route to another in React? 如何将 useState 从一个组件传递到另一个组件? - How do I pass useState from one component to another? 如何将变量从一个组件传递到另一个组件? - How do I pass a variable from one component to another? 如何以角度将数据从一个组件传递到另一个组件(新浏览器选项卡)? - How do I pass data from one component to another (New Browser Tab) in angular? Reactjs-如何将数据或变量从一个组件传递到另一个组件 - Reactjs- How do I pass data or a variable from one component to another 如何将数据从一个组件传递到另一个组件? - How to pass data from one component to another component? 如何在reactjs中将数据数组从一个组件传递到另一个组件 - How to pass array of data from one component to another component in reactjs 如何将数据输入从一个组件传递到另一个组件? - How to pass the data input from one component into another component? 如何将值从一个组件传递到另一个组件? - How can I pass values from one component to another component? 如何在 React.JS 中将数据从一个组件传递到另一个组件 - How to pass data from one component to another in React.JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM