reactjsreduxreact-reduxredux-reducersredux-actions

How to display username on navbar after login in react redux


There is a nav link on my navbar named Login (I have used bootstrap navbar). After successful login I want to replace Login with a username using react-redux (not redux-toolkit). Kindly help me in writing reducer and action code. As I am a beginner in redux and I don't know how to code in reducer and action file it is very confusing for me.

Navbar component (Navbar.js)

import React from 'react';
import { Link } from 'react-router-dom';
import { useSelector } from 'react-redux';

const Navbar = () => {
const {username} = useSelector((state) => state.user);
    return ( 
        <div>
            <nav className="navbar navbar-expand-lg">
                <div className="container-fluid">
                    <Link className="navbar-brand" to="/hommepage">Ebuy</Link>
                    <button className="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-                        expanded="false" aria-label="Toggle navigation">
                        <span className="navbar-toggler-icon">
                            <i className="fas fa-bars" style={{color: 'white', fontSize: '28px'}}></i>
                        </span>
                    </button>
                    <div className="collapse navbar-collapse" id="navbarSupportedContent">
                        <ul className="navbar-nav me-auto mb-2 mb-lg-0">
                            <li className="nav-item">
                                <Link className="nav-link" aria-current="page" to="/homepage">Home</Link>
                            </li>
                            <li className="nav-item dropdown">
                                <Link className="nav-link dropdown-toggle" to="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                                Select Category
                                </Link>
                                <ul className="dropdown-menu" aria-labelledby="navbarDropdown">
                                    <li><Link className="dropdown-item" to="/skincare">Skincare Products</Link></li>
                                    <li><Link className="dropdown-item" to="/stationary">Stationary Products</Link></li>
                                    <li><Link className="dropdown-item" to="#">Clothing</Link></li>
                                </ul>
                            </li>
                        </ul>
                        <div className="d-flex">
                            <ul className="navbar-nav me-auto mb-2 mb-lg-0">
                                <li className='nav-item'>
                                    <Link className="nav-link" to="/">Login</Link>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </nav>
        </div>
    );
};
 
export default Navbar;

Solution

  • reducer

    const initialState = {
      username: null,
    }
    
    export  const userReducer(state = initialState, action) {
      switch (action.type) {
        case "CHANGE_USERNAME":
          return {
            ...state,
            username: action.payload,
          }
        default:
          return state
      }
    }
    
    

    action

    export const changeUserName = (name) => {
     return {
      type: "CHANGE_USERNAME",
      payload: name
     }
    }
    
    
    

    after login call this

    
    const dispatch = useDispatch()
    
    const login = () => {
     ... login logic here
     
     dispatch(changeUserName("user-1"))
    
    }
    
    
    

    in navbar

    const { username } = useSelector(state=> state.user)