我有一个连接到商店的组件。 该组件调度一个Action并正确更改状态(see Logs)。 但是我的组件应该显示此状态更改。但是它不会重新呈现。
这是我的代码:
我的Redux商店,reducer和Action就像这样:
//store.js
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk'
import { logger } from 'redux-logger'
import reducer from "./src/reducers/index"
const middleware=applyMiddleware(thunk, logger)
export default store = createStore(reducer, middleware);
//reducers/index.js
const initialState = {
isLoggedIn:false,
}
export default loginReducer=(state = initialState, action) => {
switch (action.type) {
case "LOGGED_IN":
return { ...state, isLoggedIn:true };
default:
return state
}
};
//actions.js
export const logIn = (token) => ({
type: "LOGGED_IN",
payload: token
})
我的React Native应用程序是这样包装的:
import { AppRegistry } from 'react-native';
import App from './src/App';
import React from 'react';
import { Provider } from 'react-redux';
import store from './store'
/**
* Wrap App in Redux Provider
*/
const MainApp = () => (
<Provider store={store}>
<App />
</Provider>
)
AppRegistry.registerComponent('SalesforcegoesmobleReactNative', () => MainApp);
最后,我希望我的组件在状态更改后更新状态并重新渲染:
import React, { Component } from 'react';
import { View, Text, Button } from 'react-native';
import { connect } from "react-redux"
import { logIn } from '../actions/actions';
@connect(
(state) => {
return {
isLoggedIn: state.isLoggedIn
}
},
(dispatch) => {
return {
updateLogin: id => dispatch(logIn(id))
}
}
)
export class InvoiceList extends Component {
render() {
console.log("rendering...")
return (
<View>
<Button title="Update Login" onPress={() => this.props.updateLogin("test")} />
<Text>{this.props.isLoggedIn ? "true" : "false"}</Text>
</View>
);
}
}
因此您可以看到状态正确更改。 但是之后,我希望组件再次呈现(控制台和文本标签中的“呈现...”显示为true而不是false)
我没看错。你能帮我吗?
答案 0 :(得分:2)
尝试像在connect
中那样定义isLoggedIn值:
(state) => {
return {
isLoggedIn: state.loginReducer.isLoggedIn
}
}