请注意,当列表出现时,我直接单击了第一项,但是渲染需要5秒钟...从列表中删除项目时,我遇到了同样的问题:https://imgur.com/1ChZYBy。
退出调试模式时,列表出现时响应速度更快,但是删除项目时响应速度仍然很慢。
TripCell
和typedTripList
从类转换为功能组件shouldComponentUpdate() { return false }
放在几乎我可以使用的任何地方TripCell
渲染了很多次TripList
中实现? TripListAllScreen
是显示所有行程的屏幕
TripListDriverScreen
是一个屏幕,显示用户作为驾驶员的所有行程
应该最多提供62个渲染,因为这里LIST_ALL
包含与LIST_DRIVER
相同的内容,我总共有26次行程。
export default class TripListAllScreen extends Component {
render() {
console.log(`RENDER : ${this.constructor.name}`);
return (
<View style={[{ flex: 1 }]}>
<TripList listType={LISTE_ALL} />
</View>
);
}
}
function typedTripList(WrappedTripList) {
return function TypedTripList(props) {
const {
listType, dataList, setDataList, setSelectedTrip, setSelectedGeneralTrip, token,
} = props;
let route;
switch (listType) {
case LISTE_ALL:
route = '/trip/get';
break;
case LIST_DRIVER:
route = '/trip/getWhereUserIsDriver';
break;
default:
route = '/trip/get';
}
console.log(`typedTripList.${props.listType}`);
return (
<WrappedTripList
route={route}
dataList={dataList}
setDataList={setDataList}
setSelectedTrip={setSelectedTrip}
setSelectedGeneralTrip={setSelectedGeneralTrip}
token={token}
/>
);
};
}
const mapStateToProps = (state, ownProps) => ({
// Determine the redux dataList to use <all trips | where user is driver | where user is passenger>
dataList: state.datalist[ownProps.listType],
token: state.token.token,
});
const mapDispatchToProps = (dispatch, ownProps) => ({
// Determine the redux dataList to use in set data list action
// <all trips | where user is driver | where user is passenger>
setDataList: data => dispatch(setDataListAction(data, ownProps.listType)),
setSelectedTrip: idRecurrence => dispatch(setSelectedTripAction(idRecurrence)),
setSelectedGeneralTrip: idTrajet => dispatch(setSelectedGeneralTripAction(idTrajet)),
});
typedTripList.propTypes = {
listType: PropTypes.string.isRequired,
};
// Fixed the problem ! :)
const composedHoc = compose(
connect(mapStateToProps, mapDispatchToProps),
typedTripList,
);
export default composedHoc;
class TripList extends Component {
state = {
isReady: false,
refreshing: false,
}
constructor(props) {
super(props);
this._getTrips();
}
// Trips should be got in typedTripList (OR MAYBE NOT: THEN DIFFICULT TO MANAGE LOADING)
_getTrips = () => {
const { token, setDataList, route } = this.props;
// console.log('GETTING TRIPS');
// console.log(`TOKEN: ${token}`);
protectedGetRequest(route, token) // '/trip/get'
.then(response => response.json())
.then((json) => {
this.setState({ isReady: true });
setDataList(json.data);
})
.then(() => {
this.setState({ refreshing: false });
})
.catch(e => console.log(e));
}
_renderTripCell = ({ item }) => (
<TouchableOpacity onPress={() => this._navToTripDetails(item.idRecurrence, item.Trajet.idTrajet)}>
<TripCell
start={item.Trajet.EtapeTrajets[0].Etape.Nom}
arrival={item.Trajet.EtapeTrajets[item.Trajet.EtapeTrajets.length - 1].Etape.Nom}
datetime={this._renderDatetime(item)}
seats={item.Trajet.NbPlaces}
passengers={item.NbPassagers}
/>
</TouchableOpacity>
);
_renderDatetime = (item) => {
const date = mysqlToJsDate(item.Date);
const dateTime = mysqlToJsDateTime(item.Trajet.HeureDepart);
return {
...date,
hours: dateTime.hours,
minutes: dateTime.minutes,
};
}
_navToTripDetails = (idRecurrence, idTrajet) => {
const { navigation, setSelectedTrip, setSelectedGeneralTrip } = this.props;
const { navigate } = navigation;
// TODO: merge those functions
setSelectedTrip(idRecurrence);
setSelectedGeneralTrip(idTrajet);
navigate('TripDetails');
}
render() {
console.log(`RENDER : ${this.constructor.name}`);
const { isReady, refreshing } = this.state;
const { dataList } = this.props;
if (!isReady) return (<Loading />);
return (
<View style={[styles.container]}>
<FlatList
data={dataList}
keyExtractor={(item, index) => `${item.idRecurrence}-${index}`}
renderItem={this._renderTripCell}
refreshControl={(
<RefreshControl
refreshing={refreshing}
onRefresh={() => { this._getTrips(); }}
/>
)}
/>
</View>
);
}
}
export default typedTripList(withNavigation(TripList));
case DELETE_ITEM:
console.log('in delete item');
return {
...state,
[LISTE_ALL]: state[LISTE_ALL].filter(item => item.idRecurrence != action.payload.idRecurrence),
[LIST_DRIVER]: state[LIST_DRIVER].filter(item => item.idRecurrence != action.payload.idRecurrence),
[LIST_PASSENGER]: state[LIST_PASSENGER].filter(item => item.idRecurrence != action.payload.idRecurrence),
};