简体   繁体   English

在使用 React 渲染之前初始化数据

[英]initializing data before rendering using React

I'm a beginner in react native.我是反应原生的初学者。 In my project, I need to set data before rendering it and return the data.在我的项目中,我需要在渲染之前设置数据并返回数据。

Can help me?可以帮我? thanks.谢谢。

async function ReadData() {
   var RNFS = require('react-native-fs');
try {
if (await RNFS.exists(RNFS.DocumentDirectoryPath + '/logindata.json')) {
  var path = RNFS.DocumentDirectoryPath + '/logindata.json';
  var Status = await RNFS.readFile(path, 'utf8')
  var login = JSON.parse(Status)
  if (login.is_login == true) {
    return true
  } else {
    return false
  }
 } else {
  return false
 }
} catch{
 return false
}
}
const App = () => {
useEffect( () => {
var Login=ReadData()    
SetDirection();
SplashScreen.hide();
console.log(Login)
}, []);

This is not the way it works with React.这不是它与 React 一起工作的方式。 React will render your UI immediately and update automatically when the data is loaded or updated. React 将立即呈现您的 UI,并在数据加载或更新时自动更新。

However, you can hide the splash screen when the data is finished loading.但是,您可以在数据加载完成后隐藏启动画面。 App can pass a callback to ReadData and ReadData will call this callback when data is ready, then App can hide the splash screen. App 可以给 ReadData 传递一个回调,ReadData 会在数据准备好时调用这个回调,然后 App 可以隐藏闪屏。

App应用程序

useEffect( () => {
  const Login=ReadData(onDataReady)
  ...

onDataReady = () => {
  SplashScreen.hide();
  ....
}

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

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