[英]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谢谢
<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 )}/> );
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.