gpt4 book ai didi

performance - React Native 与 Redux 的低性能

转载 作者:行者123 更新时间:2023-12-03 13:37:20 25 4
gpt4 key购买 nike

我发现我的 React Native 应用程序存在一些性能问题。这似乎是由react-redux包引起的。

正如您在视频中看到的

Redux/Flux/setState comparing

Action 调度和 View 渲染之间存在明显的延迟。在真实设备上,情况看起来更糟。此示例中没有 API 调用。只有简单的 Action 调度和状态改变。另一方面,Facebook Flux 的实现和简单的 setState 调用工作得更快。

关于如何提高应用程序性能有什么想法吗?

我正在使用 react :v15.2.1, react native :v0.29.2, react -redux:v4.4.5,

查看

import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';

import {Map} from 'immutable';

import * as testActions from '../reducers/test/testActions';
import {Actions} from 'react-native-router-flux';

const actions = [
testActions
];

function mapStateToProps(state) {
return {
...state
};
}

function mapDispatchToProps(dispatch) {
const creators = Map()
.merge(...actions)
.filter(value => typeof value === 'function')
.toObject();

return {
actions: bindActionCreators(creators, dispatch),
dispatch
};
}

........

<View style={styles.box}>
{PRICE_FILTERS.map(filter =>{
let price_cont_style = {};
let price_text_style = {};
if (filter.id == PRICE_FILTERS[3].id){
price_cont_style.marginRight = 0;
}
if (filter.id == this.props.test.price){
price_cont_style.backgroundColor = 'black';
price_text_style.color = 'white';
}
return(
<TouchableOpacity
key={filter.id}
style={[styles.price_cont, price_cont_style]}
onPress={()=>this.props.actions.setPrice(filter.id)}>
<Text
style={[styles.price_text, price_text_style]}>
{filter.label}
</Text>
</TouchableOpacity>
);
})}
</View>

......

export default connect(mapStateToProps, mapDispatchToProps)(PerformanceTest);

操作

const {
TEST_SET_PRICE,
} = require('../../lib/constants').default;

export function setPrice(filter) {
return {
type: TEST_SET_PRICE,
payload: filter
};
}

reducer

const {
TEST_SET_PRICE,
} = require('../../lib/constants').default;
const InitialState = require('./testInitialState').default;
export default function authReducer(state = InitialState, action) {
switch (action.type) {

case TEST_SET_PRICE:
if (state.price!=action.payload){
return {price:action.payload}
} else{
return state;
}

}
return state;
}

最佳答案

事实证明,此问题的原因是导航链中的所有组件均未安装并在当前场景后面重新渲染

在此处查看更多详细信息 Possible navigation issue in React Native/Redux app

关于performance - React Native 与 Redux 的低性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38575596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com