我剛開始學習反應本地和終極版。我正在嘗試實現身份驗證方案。我正在使用react-navigation
。使用反應導航與終極版的反應天然的「不能現有日期過渡期間更新」
import { authenticate } from './../actions';
const Component = (props) => {
const { dispatch, user } = props;
// user comes from state (using mapStateToProps)
let phone = '';
let password = '';
if (user.errors != null) {
alert(user.errors);
}
if (user.authenticated) {
dispatch(NavigationActions.navigate({ routeName: 'LoggedIn' }));
}
async function onLogin() {
dispatch(authenticate(phone, password));
}
return (
<View>
<TextInput
style={{height: 40, width: '100%', borderColor: 'gray', borderWidth: 1, marginTop: 10}}
placeholder="(xxx) xxx-xx-xx"
keyboardType={'phone-pad'}
onChangeText={(value) => phone = value}
value={phone}
/>
<TextInput
style={{height: 40, width: '100%', borderColor: 'gray', borderWidth: 1, marginTop: 10}}
placeholder="Пароль"
onChangeText={(value) => password = value}
value={password}
/>
<Button
onPress={onLogin}
title="Continue"
/>
</View>
);
}
這裏是驗證行動
export function authenticate(phone, password) {
return async dispatch => {
dispatch(authenticating());
let response;
try {
response = await Api.authenticate(phone, password);
}
catch (e) {
dispatch(authenticateFailure(e.error));
return;
}
dispatch(authenticateSuccess(response.token, response.payload));
};
};
時authenticate
動作發出的,成功驗證用戶身份所以基本上,我想用戶重定向到下一屏(LoggedIn
)。但重定向後,我得到這個錯誤:
Warning: Cannot update during an existing state transition (such as within render or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to componentWillMount.
我猜問題是,以前的組件中斷,當它被重定向到下一個組件(的loggedIn)被渲染,因爲每一個調度觸發重新呈現。
如何導航到另一個屏幕上的動作被分派後?我可以以某種方式派發一個動作而不觸發重投嗎?
這是行不通的。 'componentWillMount'只執行一次,但我需要捕獲更新的狀態。應該有另一種方式。我也想使用基於功能的組件,而不是基於類。 – staskjs
啊我明白了,那麼你可以把它放在componentWillReceiveProps –
你也可以選中要設置觸發特定行爲 –