score:0

// const temp = JSON.parse(localStorage.getItem('Name', 'Image') || '[]');
const temp = JSON.parse(localStorage.getItem('Name') || '[]');

const Posts = () => {
  const [state, setState] = React.useState(temp);

  const deleteItem = removeindex => {
    // localStorage.removeItem('Name');
    // localStorage.removeItem('Image');

    setState(s => s.filter((_, index) => index !== removeindex));
  };

  React.useEffect(() => {
    localStorage.setItem('Name', JSON.stringify(state));
  }, [state]);

  return (
    <div className="post-data">
      <div className="post-data-content">
        {temp.map((val, index) => (
          <div className="posts-data" key={index}>
            <button type="button" className="delete-post" onClick={() => deleteItem(index)}>
              x
            </button>
            <img src={val.image} alt={val.image} />
            <h3>{val.name}</h3>
          </div>
        ))}
      </div>
    </div>
  );
};

score:0

import { useNavigate } from 'react-route-dom';

const Posts = () => {

const navigate = useNavigate();

const temp = JSON.parse(localStorage.getItem("Name","Image") || "[]");

const deleteItem = (index) =>{
    localStorage.removeItem("Name");
    localStorage.removeItem("Image");
    navigate('/login'); //like here
}

return (
  <>
    <div className='post-data'>
      <div className='post-data-content'>
         {
            temp.map((val,index)=>{
               return(
                    <>
                      <div className='posts-data' key={index}>
                        <button type='button' className="delete-post" onClick={deleteItem}>x</button>
                          <img src={val.image} alt={val.image}/>
                             <h3>{val.name}</h3>
                                </div>
                                </>
                            )
                       })
                   }
                </div>
            </div> 
    </div>
</>
)
}

Related Query

More Query from same tag