首先你不需要储存
userToken
在状态中,因为您不在本地修改它,因此不需要
getDerivedStateFromProps
componentDidUpdate
因此再次调用API会导致无限循环
第三,shouldComponentUpdate比较props中的检查不完全正确,因为nestedObjects props将给出一个假阴性结果,而且如果您为props编写一个深度相等检查,则如果状态更改,组件将不会重新呈现。
// imports
class HomeScreen extends Component {
static navigationOptions = {
header: null,
};
state = {
error: false,
};
componentDidMount() {
this.GetPassengersData();
}
componentDidUpdate(prevProps, prevState) {
if (prevProps.userToken !== this.props.userToken) {
this.GetPassengersData();
}
}
GetPassengersData = async () => {
const { passengersDataActionHandler, userToken } = this.props;
if (userToken && userToken !== null) {
try {
const response = await fetch(
'http://myAPI/public/api/getPassengers',
{
method: 'POST',
headers: {
Authorization: `Bearer ${userToken}`,
Accept: 'application/json',
'Content-Type': 'application/json',
},
},
);
const responseJson = await response.json();
if (has(responseJson, 'error')) {
this.setState({ error: true });
Alert.alert('Error', 'Please check your credentials.');
} else {
passengersDataActionHandler(responseJson.success.data);
}
} catch (error) {
this.setState({ error: true });
Alert.alert(
'Error',
'There was an error with your request, please try again later.',
);
}
}
};
render() {
return <TabView style={styles.container} />;
}
}
HomeScreen.defaultProps = {
userToken: null,
};
HomeScreen.propTypes = {
navigation: PropTypes.shape({}).isRequired,
passengersDataActionHandler: PropTypes.func.isRequired,
userToken: PropTypes.oneOfType([PropTypes.string]),
};
export default compose(
connect(
store => ({
userToken: store.signinScreen.userToken,
passengersData: store.homeScreen.passengersData,
}),
dispatch => ({
passengersDataActionHandler: token => {
dispatch(passengersDataAction(token));
},
}),
),
)(HomeScreen);