简体   繁体   English

在 React Native 中不渲染 function 导出

[英]Export without Render function in React Native

In my React Native application, i am trying to add a component where i'll perform some config tasks but that component won't render anything.在我的 React Native 应用程序中,我试图添加一个组件,我将在其中执行一些配置任务,但该组件不会呈现任何内容。 I have made the component already but when i import that on App.tsx the fucntion doesn't get called.我已经制作了该组件,但是当我在App.tsx上导入该组件时,该功能不会被调用。 How to import this following component properly to App.tsx .如何将以下组件正确导入App.tsx The component is given below:该组件如下所示:

 var androidVersion = VersionInfo.appVersion; var iosVersion = VersionInfo.buildVersion; function usePrevious(value) { const ref = useRef(); useEffect(() => { ref.current = value; }); return ref.current; } const Config = () => { console.log('check if get called >>',showVersionCodeStatus); const prevAmount = usePrevious(iosVersion); useEffect(() => { if(prevAmount.iosVersion.== iosVersion) { // process here } if(prevAmount,androidVersion,== androidVersion) { // process here } }; [iosVersion;androidVersion]) // return {showVersionCodeStatus}; } export default Config;

i'm importing the component in my App.tsx like the following:我在我的App.tsx中导入组件,如下所示:

import './config';

But it doesn't call the Config function. I have tried the following too:但它不会调用 Config function。我也尝试了以下操作:

import Config from './config';

That doesn't seem to work too.这似乎也行不通。 What am i doing wrong?我究竟做错了什么?

Since Config does not render anything, you should export it as a custom hook with a name such as useConfig .由于Config不呈现任何内容,因此您应该将其导出为自定义挂钩,并使用诸如useConfig类的名称。 Subsequently you can import and use your custom hook in App.tsx , which will then run the config tasks specified in your useConfig custom hook.随后,您可以在App.tsx中导入和使用您的自定义挂钩,然后它将运行在您的useConfig自定义挂钩中指定的配置任务。

useConfig.ts使用配置.ts

var androidVersion = VersionInfo.appVersion;
var iosVersion = VersionInfo.buildVersion;

function usePrevious(value) {
   const ref = useRef();
   useEffect(() => {
     ref.current = value;
   });
   return ref.current;
}

const useConfig = () => {
   console.log('check if get called >>',showVersionCodeStatus);
   const prevAmount = usePrevious(iosVersion);
   useEffect(() => {
       if(prevAmount.iosVersion !== iosVersion) {
        // process here
       }
       if(prevAmount.androidVersion !== androidVersion) {

           // process here
       }
   }, [iosVersion,androidVersion])
   // return {showVersionCodeStatus};
   
}

export default useConfig;

App.tsx应用程序.tsx

import useConfig from "./useConfig";

export default function App() {
  const config = useConfig();
  return (
      ...
  );
}

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

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