I have two redux state variables, one that hold an array of user information and one that holds a true/false value for a side drawer open/close feature. The true/false value triggers a className change which triggers CSS to open/close the drawer. The array of user information is fetched from a firebase cloud firestore database collection.
For some reason after the user array is fetched and saved to the redux state and a user opens the side drawer the redux action sent is only for the side drawer, but the side drawer and users information is changed.
The side drawer opens like normal, but the user array is set to null.
Redux Events:
Initial State: https://imgur.com/a/IgvXMLe
After side drawer is opened: https://imgur.com/a/wVRg6Az
Side Drawer Event Difference: https://imgur.com/a/u1hrcvT
Side Drawer Component
class SideDrawer extends Component {
render() {
let drawerClasses = ['side-drawer'];
if (this.props.toggled) {
drawerClasses = ['side-drawer', 'open'];
}
return (
<div className={drawerClasses.join(' ')} >
<div className="side-drawer-container" >
<div className="router-login-button" onClick={this.props.toggleSideDrawer} >
<OktaAuthButton />
</div>
<div className="side-drawer-link" onClick={this.props.toggleSideDrawer} >
<Link to="/" >Map</Link>
</div>
<div className="side-drawer-link" onClick={this.props.toggleSideDrawer} >
<Users />
</div>
</div>
</div>
)
}
}
const mapStateToProps = ({ sideDrawer }) => ({
toggled: sideDrawer.toggled,
});
const mapDispatchToProps = (dispatch) => {
return {
toggleSideDrawer: () => dispatch({ type: TOGGLE_SIDEDRAWER, payload: true })
}
}
export default connect(mapStateToProps, mapDispatchToProps)(SideDrawer);
Side Drawer Reducer
import { TOGGLE_SIDEDRAWER } from './actions';
const initialState = {
toggled: false
};
export default function sideDrawerReducer(state = initialState, action) {
switch (action.type) {
case TOGGLE_SIDEDRAWER:
return Object.assign({}, state, {
toggled: action.payload
});
default:
return state;
}
}
Users Component
class Users extends Component {
/* commented code not needed to be shown */
componentDidMount() {
initializeFirebaseApp();
// Get user list from firestore 'users' collection
this.loadUsers();
}
async loadUsers() {
getAllUsers().then((users) => {
this.props.setUsers(users);
});
}
render() {
if(this.props.users != null) {
var users = this.props.users.map((el, i) => (
<li key={el.id} className='user' onClick={this.props.toggleSideDrawer}><Link to={"/user/" + el.id}>{el.firstname}</Link></li>
));
console.log(users);
}
console.log(this.props.users);
return (
<div className="user-container">
{users}
</div>
)
}
}
const mapStateToProps = ({ users }) => ({
users: users.friends
});
const mapDispatchToProps = (dispatch) => {
return {
setUsers: (users) => dispatch({type: SET_FRIENDS, payload: users}),
toggleSideDrawer: () => dispatch({ type: TOGGLE_SIDEDRAWER, payload: false })
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Users);
Users Reducer
import { SET_FRIENDS } from './actions';
const initialState = {
friends: null,
groups: null
};
export default function userReducer(state = initialState, action) {
switch(action.type) {
case SET_FRIENDS:
return Object.assign({}, state, {
friends: action.payload
});
default:
return initialState;
}
}
I expect the side drawer to open and render the list of users in the drawer under the "Login" and "Map" Links
The default case for userReducer is returning initialState instead of state so every action through the redux store that is not SET_FRIENDS (e.g. TOGGLE_SIDEDRAWER) will reset the userReducer to initialState where users is null. Return state instead and you should be good to go.
export default function userReducer(state = initialState, action) {
switch(action.type) {
case SET_FRIENDS:
return Object.assign({}, state, {
friends: action.payload
});
// Change to `return state;`
default:
return initialState;
}
}
Related
In my app component i have list of posts that contains user id, i want to display the user name and details against that user id, here's my app component's jsx:
App Component JSX:
render() {
const posts = [...someListOfPosts];
return posts.map((post) => {
return (
<div className="item" key={post.id}>
<div className="content">
<User userId={post.userId} />
</div>
</div>
);
});
}
User Component
import React from 'react';
import { connect } from 'react-redux';
import { fetchUser } from '../actions';
class UserHeader extends React.Component {
componentDidMount() {
this.props.fetchUser(this.props.userId); // getting correct userId
}
render() {
const { user } = this.props;
// Not displaying correct user i.e. showing the last resolved user for each post
return (
<div>
{user && <div className="header">{user.name}</div>}
</div>
);
}
}
const mapStateToProps = (state, props) => {
return {
user: state.user
};
}
export default connect(mapStateToProps, { fetchUser })(UserHeader);
I'm getting correct props for userId but for every post it displays the last resolved user from the api. It should be relevant user for every post.
Reducer and Action Creator
// action
export const fetchUser = (id) => {
return async (dispatch) => {
const response = await axios.get(`https://jsonplaceholder.typicode.com/users/${id}`);
dispatch({
type: 'FETCH_USER',
payload: (response.status === 200 && response.data) ? response.data : null; // it returns single user not array of user
});
}
}
// reducer
export default (state = null, action) => {
switch (action.type) {
case 'FETCH_USER':
return action.payload; // i know it can be fixed by defaulting state to empty array and returning like so [...state, action.payload] but why should i return complete state why not just a single user object here?
default:
return state;
}
}
The fetchUser action creator returns single payload of a user not an array then why it's required to return the state like [...state, action.payload] why can't it be done by returning action.payload only? I've tried it by returning only action.payload but in my user component it displays the last resolved user from the api every time for each post. I'm confused regarding this.
You are subscribing to the store using mapStateToProps which rerenders when ever there is a change in the store. As you are trying to render via props in User component, the application retains the last value of user and re-renders all the old User Components as well. If you want to ignore the props updates make the result local to the component.
You can possibly try this:
import React from 'react';
import { connect } from 'react-redux';
import { fetchUser } from '../actions';
class UserHeader extends React.Component {
constructor(props){
super(props);
this.state={
userDetails:{}
}
}
componentDidMount() {
fetch(https://jsonplaceholder.typicode.com/users/${this.props.userId})
.then(res => res.json())
.then(
(result) => {
this.setState({
userDetails: result.data
});
},
// Note: it's important to handle errors here
// instead of a catch() block so that we don't swallow
// exceptions from actual bugs in components.
(error) => {
this.setState({
isLoaded: false
});
}
)
}
render() {
return (
<div>
{this.state.userDetails && <div className="header">{this.state.userDetails.name}</div>}
</div>
);
}
}
const mapStateToProps = (state, props) => {
return {
};
}
export default connect(mapStateToProps, { fetchUser })(UserHeader);
I have the following React component connected to the Redux store, and even though the state of the store changes (I checked), the component prop userIsLogged won't change its value. Any help is appreciated!
const mapDispatchToProps = (dispatch) => bindActionCreators({deauthenticateUser}, dispatch);
const mapStateToProps = (state) => ({ userIsLogged: state.auth.loggedUser !== null });
const Logout = (props) => {
const { userIsLogged } = props;
return (
userIsLogged?
<Button
variant="outlined"
color="primary"
onClick={(e) => {
props.deauthenticateUser();
history.push('/login');
}}>
Exit
</Button>
:<div />
);
}
Logout.propTypes = {
userIsLogged: PropTypes.bool.isRequired
};
export default connect(mapStateToProps, mapDispatchToProps)(Logout);
The reducer is as follow:
const initialState = {
jwt: null,
loggedUser: null,
isLoading: false
}
export default function auth(state = initialState, action) {
switch (action.type) {
case 'GOT_JWT':
return Object.assign(state, { jwt: action.jwt });
case 'USER_LOGGING_IN':
return Object.assign(initialState, { isLoading: action.isLoading });
case 'USER_LOGGED_IN':
return Object.assign(state, { loggedUser: action.loggedUser, isLoading: false });
case 'NO_JWT':
return initialState;
case 'USER_LOGGED_OUT':
return initialState;
default:
return state;
}
}
In your reducer code you're mutating the passed state object.
What happens next is that react treats the state as unchanged (it's the same object), hence it does not re-render it.
To fix it change the
Object.assign(state, { jwt: action.jwt });
to
Object.assign({}, state, { jwt: action.jwt });
It would create a new object and copy properties from the original state + the new ones.
I have a parent react component containing 3 children:
<ChildComponent category="foo" />
<ChildComponent category="bar" />
<ChildComponent category="baz" />
The child component calls an api depending on the prop category value:
http://example.com/listings.json?category=foo
In my action the data is returned as expected. However, when the child component renders the data. The last child baz is overwriting its value in foo and bar as well.
A solution to this problem seems to be given here. But I would like this to be dynamic and only depend on the category prop. Is this not possible to do in Redux?
My child component looks like this:
class TweetColumn extends Component {
componentDidMount() {
this.props.fetchTweets(this.props.column)
}
render() {
const { tweets, column } = this.props
if (tweets.length === 0) { return null }
const tweetItems = tweets[column].map(tweet => (
<div key={ tweet.id }>
{ tweetItems.name }
</div>
)
return (
<div className="box-content">
{ tweetItems }
</div>
)
}
}
TweetColumn.propTypes = {
fetchTweets: PropTypes.func.isRequired,
tweets: PropTypes.array.isRequired
}
const mapStateToProps = state => ({
tweets: state.tweets.items
})
export default connect(mapStateToProps, { fetchTweets })( TweetColumn )
reducers:
export default function tweetReducer(state = initialState, action) {
switch (action.type) {
case FETCH_TWEETS_SUCCESS:
return {
...state,
[action.data[0].user.screen_name]: action.data
}
default:
return state;
}
}
export default combineReducers({
tweets: tweetReducer,
})
action:
export const fetchTweets = (column) => dispatch => {
dispatch({ type: FETCH_TWEETS_START })
const url = `${ TWITTER_API }/statuses/user_timeline.json?count=30&screen_name=${ column }`
return axios.get(url)
.then(response => dispatch({
type: FETCH_TWEETS_SUCCESS,
data: response.data
}))
.then(response => console.log(response.data))
.catch(e => dispatch({type: FETCH_TWEETS_FAIL}))
}
You are making an api call every time TweetColumn is mounted. If you have multiple TweetColumn components and each one makes an api call, then whichever one's response is last to arrive is going to set the value of state.tweets.items. That's because you are dispatching the same action FETCH_TWEETS_SUCCESS every time (the last one overrides the previous one). To solve that issue, assuming the response has a category (foo, bar, baz), I would write the reducer in the following way:
export default function tweetReducer(state = initialState, action) {
switch (action.type) {
case FETCH_TWEETS_SUCCESS:
return {
...state,
[action.data.category]: action.data
}
default:
return state;
}
}
You can then do the following in your TweetColumn component:
class TweetColumn extends Component {
componentDidMount() {
this.props.fetchTweets(this.props.column)
}
render() {
const { column } = this.props;
const tweetItems = this.props.tweets[column].map(tweet => (
<div key={ tweet.id }>
{ tweet.name }
</div>
)
return (
<div className="box-content">
{ tweetItems }
</div>
)
}
}
const mapStateToProps = state => ({
tweets: state.tweets
})
const mapDispatchToProps = dispatch => ({
fetchTweets: column => dispatch(fetchTweets(column))
})
export default connect(
mapStateToProps,
mapDispatchToProps,
)( TweetColumn )
You will have to do some validation to make sure tweets[column] exists, but you get the idea.
Have a component to display user information. However, when the user logouts out, and shouldn't be in the store anymore ( I have set a dispatch up for this as well). Also, I am able to reload the entire page and then the user information displays. I have been having a go with componentDidUpdate and componentDidMount but I can't seem to figure it out.
Here is the view component:
// import React from "react";
// import { connect } from "react-redux";
// import { getUser } from "../store/actions/userActions";
// import { withRouter } from 'react-router-dom';
import React from 'react';
import { connect } from 'react-redux';
import * as actions from '../store/actions/auth';
class UserDetailView extends React.Component {
componentDidMount() {}
shouldComponentUpdate(nextProps, props) {
console.log(nextProps);
const username = this.props.user.username;
console.log(username);
if (username !== nextProps.username) {
console.log(username);
return true;
} else {
return false;
}
}
render() {
const user = this.props.user;
return (
<div>
{this.props.user ? (
<div>
<h3>{user.username}</h3>
{this.props.user.email}
</div>
) : (
<h3>Not Logged In</h3>
)}
</div>
);
}
}
const mapStateToProps = state => ({
username: state.username,
user: state.user
});
const mapStateToDispatch = dispatch => ({
onTryAutoSignup: () => dispatch(actions.authCheckState()),
getfetchUser: id => dispatch(actions.fetchUser(id))
});
export default connect(
mapStateToProps,
mapStateToDispatch
)(UserDetailView);
// class UserDetailView extends React.Component {
// componentDidMount() {
// const { getUser, userID } = this.props
// getUser(userID) //fixed
// }
// render() {
// console.log(this.props.userID)
// console.log(this.props.user)
// return (
// <ul>
// {this.props.user.map(user =>
// <li key={user.id}>{user.username}</li>
// )}
// </ul>
// );
// }
// }
// const mapStateToProps = (state, ownProps) => ({
// user: state.user,
// userID: ownProps.match.params.userID,
// });
// const mapDispatchToProps = dispatch => ({ //added
// getUser: (userID) => dispatch(getUser(userID))
// })
// export default withRouter(connect(mapStateToProps, {getUser})(UserDetailView)); //fixed
Reducer:
const getUserInformation = (state, action) => {
return Object.assign({}, state, {
user: action.payload.user
});
};
Action Generator and Action
export const authSuccess = (token, username) => {
return {
type: actionTypes.AUTH_SUCCESS,
token: token,
username: username
};
};
export const fetchUser = username => {
return dispatch => {
return axios
.get(`http://127.0.0.1:8000/api/user/${username}/`)
.then(res => {
const user = res.data;
dispatch(getUserInformation(user));
});
};
};
I see no reason to override shouldComponentUpdate, just inherit from React.PureComponent.
You have some mix-ups in action creators and reducers. It should be something like this:
dispatch(setUserInformation(user)); // dispatch action
const setUserInformation = ({ type: 'SET_USER_INFORMATION', user }); // this is the action creator, returns an object with the type and the payload
const reducer = (state, action) { // this is the reducer
switch (action.type) {
case 'SET_USER_INFORMATION':
return {
...state,
user: action.user
}
}
}
The action dispatch is not working, The function works and I get the console.log but the store isn't changing. Any ideas?
import React from 'react';
import { connect } from 'react-redux';
import { NavLink } from 'react-router-dom';
import RemoveTodo from './RemoveTodo';
import { remove } from '../actions/Todo';
import { store } from '../app';
class TodosSummary extends React.Component {
constructor(props) {
super(props);
}
onDelete = ({id}) => {
store.dispatch(remove({id}))
console.log(store.getState());
};
render () {
return (
<ul>
{this.props.target.map(({todo, significance, id}) => {
return (
<li
key={id}>{todo} - impact is {significance}
<button onClick={this.onDelete}>Remove</button>
</li>
);
})}
</ul>
</div>
);
}
};
const mapStateToProps = (state) => {
return {
target: state.target
}; };
export default connect(mapStateToProps)(TodosSummary)
This is the action, taking the todo id
export const remove = ({id}) => ({
type: 'REMOVE_TODO',
id
});
And that's the reducer, filtering the state and bringing back the filtered array
const todosReducer = (state = todosReducerDefaultState, action) => {
switch(action.type) {
case 'ADD_TODO':
return [
...state,
action.target
];
case 'REMOVE_TODO':
return (
state.filter(({ id }) => id !== action.id)
);
I see you want to access target property inside state. So, the reducer should be like this:
case 'ADD_TODO':
return {
...state,
target: [...state.target, action.target]
};
case 'REMOVE_TODO':
return {
...state,
target: state.target.filter(({ id }) => id !== action.id)
};
See if this works.
Return updated state like this.
case 'REMOVE_TODO':
return [...state.filter(({ id }) => id !== action.id)];
It's not suggested to change the state directly, please change the addToDo to the following.
Object.assign({}, state, {
todoList: state.target
});
Can you please provide the code snippet for the default state as per your code 'todosReducerDefaultState'?