我的React Native应用程序在光模式下落后,但在黑暗中超快
我正在创建一个带有浅色模式的React Native应用程序。应用程序中所有组件的颜色均由我的自定义挂钩控制:
export const useColors = (): UseColorsResult => {
const theme = useColorScheme();
const isDarkTheme = theme === 'dark';
const colors = useCallback(
(entity: ColorTypes) => {
const entityColors = entityToColorsMapper[entity];
const colorKey = entityColors[isDarkTheme ? 1 : 0] || entityColors[0];
return colorsObject[colorKey];
},
[isDarkTheme],
);
return { colors, isDarkTheme };
};
EntityTocolorSmapper对象值看起来像
{
text: ['black', 'white'],
subtext: ['codGray', 'concrete'],
}
,ColorsObject看起来像是
{
white: '#FFFFFF',
black: '#000000',
concrete: '#F2F2F2',
codGray: '#252525',
}
在组件中我通常会执行这样的操作:
const AutoColoredText: FC<TextProps> = (props) => {
const { colors } = useColors();
const textStyle = {color: colors('text')}
return <Text style={textStyle} {...props}></Text>
}
事实是,当我启动应用程序时(带有所有优化版本版本, )在我的三星Galaxy S10(Android 12)手机上,在系统灯模式下,它非常矮小且FPS(滚动,屏幕更改等),
最主要的是,在黑暗模式下启动时,没有任何类似的事情发生呢
更有趣的一件事是:
- 如果我以 dark模式启动该应用,然后在使用该应用程序时将其更改为 light ,
- 如果我在光模式,然后将其更改为 dark 在使用该应用程序时,它仍然很懒惰,
我完全不了解,甚至无法猜测发生了什么以及为什么。
ps 在另一台没有12个Android的手机上一切正常。
upd:我已经设置isdarktheme = true
作为常量值,然后在系统灯模式下启动了该应用程序,它很laggy!在系统的黑暗模式下,它很快,所以问题绝对不在我的渲染中或其他任何与反应有关的员工
I am creating a React Native app with light and dark modes. The colors of all components in the app are controlled with my custom hook:
export const useColors = (): UseColorsResult => {
const theme = useColorScheme();
const isDarkTheme = theme === 'dark';
const colors = useCallback(
(entity: ColorTypes) => {
const entityColors = entityToColorsMapper[entity];
const colorKey = entityColors[isDarkTheme ? 1 : 0] || entityColors[0];
return colorsObject[colorKey];
},
[isDarkTheme],
);
return { colors, isDarkTheme };
};
entityToColorsMapper object values looks like
{
text: ['black', 'white'],
subtext: ['codGray', 'concrete'],
}
and colorsObject looks like
{
white: '#FFFFFF',
black: '#000000',
concrete: '#F2F2F2',
codGray: '#252525',
}
Then inside the component I usually do something like this:
const AutoColoredText: FC<TextProps> = (props) => {
const { colors } = useColors();
const textStyle = {color: colors('text')}
return <Text style={textStyle} {...props}></Text>
}
The thing is that when I launch my application (release version with all optimizations) on my Samsung Galaxy S10 (Android 12) phone in the system light mode, it is very laggy and low fps (on scrolling, screens changing, etc.)
The main thing is that nothing similar happens when it is launched in the dark mode!
One more interesting thing is:
- if I launch the app in the dark mode and then change it to the light while using the app, it remains fast
- if I launch the app in the light mode and then change it to the dark while using the app, it remains laggy
I completely do not understand and can't even guess what is happening and why.
P.S. Everything works fine on another phone with not 12 Android.
UPD: I've set isDarkTheme = true
in the hook as constant value and then launched the app in the system light mode and IT WAS LAGGY!, after that I launched it in the system dark mode and IT WAS FAST, so the problem is absolutely not in my render perfomance or any other React-related staff
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一切都比看起来要容易得多:我的光线模式的飞溅屏幕图像以某种方式具有很高的分辨率(接近7000 x 3000像素),而每一侧的黑暗模式都低两次(3500 x 1500)。
Splash屏幕映像还设置为Android主题中的应用背景(用于Splash和App之间的平稳过渡)。
当我注意到它并将光模式的飞溅屏幕图像分辨率更改为与夜间模式相同时,应用程序变得迅速。这就是
我要以一种方式来帮助可能与我相同问题的人们发布此答案:(
Everything was much easier than it seemed: my splash screen image for the light mode somehow had a very high resolution (near 7000 x 3000 pixels), while the dark mode was twice lower for every side (3500 x 1500).
The splash screen image was also set as the app background in Android themes (for the smooth transition between splash and app).
When I noticed it and changed the splash screen image resolution for the light mode to the same as the night mode has, the app became fast. That's all
I am posting this answer in a way to help the people that may have the same problem as I did :(
因此,您很幸运地找到了答案!将来可能会有所帮助的是React Dev工具扩展中的火焰图!您可以为多个支持的浏览器下载它,并在F12菜单中访问FlameGraph!它显示了哪个元素需要在页面上渲染多长时间,包括其子元素的加载时间:)
So, you luckily found the answer! What may help in the future, is the flamegraph in the react dev tools extension! You can download it for Multiple supported browsers and access the flamegraph in the f12 menu! There it shows which element is taking how long to render on the page, including the loading times of its child elements :)