0

I am trying to use a React icon for onClick navigation to my homepage. Button onClick works for this but when I use the same code for a react icon, I get a blank page. The commented out section is the working button onClick.

import { useNavigate } from 'react-router';
import { Bix } from 'react-icons/bi';
export default function BlackWhite() {
  const navigate = useNavigate();

  const handleClick = () => {
    navigate('/');
  };
  return (
    <div className="BlackWhite">
      <h1>Black and white</h1>
      //<button onClick={handleClick}>Back to homepage</button>
      <Bix onClick={handleClick}></Bix>
    </div>
  );
}
``

transpose
  • 9
  • 2

3 Answers3

1

Your icon name is wrong. Your are importing Bix but react-icons provide BiX. Capital X. You should check console why you are getting error. Better way is to click and copy icon name when you are selecting icons from react-icons.


import { useNavigate } from 'react-router';
import { BiX } from 'react-icons/bi';
export default function BlackWhite() {
    const navigate = useNavigate();

    const handleClick = () => {
        navigate('/');
    };
    return (
        <div className="BlackWhite">
            <h1>Black and white</h1>
            {/* <button onClick={handleClick}>Back to homepage</button> */}
            <BiX className='icon' onClick={handleClick}/>
        </div>
    );
}
sahilatahar
  • 568
  • 2
  • 13
1
import { useNavigate } from 'react-router-dom';
0
  • try with this :

    import { useNavigate } from 'react-router-dom';
    import { BiX } from 'react-icons/bi';
    
    export default function BlackWhite() {
     const navigate = useNavigate();
    
     const handleClick = async (request) => {
      navigate('/'); or
      navigate('/home');
      };
     return (
      <div className="BlackWhite">
          <h1>Black and white</h1>
          {/* <button onClick={handleClick}>Back to homepage</button> */}
          <BiX className='icon' onClick={event=>{handleClick(option)}}/>
      </div>
      );
      }