Reputation: 329
I have a component connected to my store. This component dispatches an Action and changes the state correctly (see Logs). But my Component should display this state change. But it doesn´t rerender.
Here is my code:
My Redux store, reducer and Action is like this:
//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
})
My React Native app is wrapped like this:
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);
And finally my Component I expect to update state and rerender after state-change:
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>
);
}
}
So as you can see the state changes correctly. But after that I expect the component to render again ("rendering..." in console and in the Text-Tags to show true instead of false)
I dont see the mistake. Can you help me please?
Upvotes: 0
Views: 485
Reputation: 17638
Try defining your isLoggedIn value like that in your connect
:
(state) => {
return {
isLoggedIn: state.loginReducer.isLoggedIn
}
}
Upvotes: 2