fix ERROR
can't access property "style", document.querySelector(...) is null in:
import React from 'react'
import "../styles/Body.css";
import { useState } from 'react';
import { store } from '../productsStore/Store';
import HoverImage from "react-hover-image/build"
import { Link } from 'react-router-dom';
The error you're encountering, "can't access property 'style', document.querySelector(...) is null," happens when you attempt to manipulate an element's style properties before it's available in the DOM or when the selector used in document.querySelector(...) does not match any elements in the DOM. This can often occur in React applications if the code attempting to modify the DOM directly is executed before the component has been properly rendered, or if conditional rendering prevents the targeted elements from being present in the DOM at the time the code runs.
To fix this error in a React application, especially one that uses conditional rendering like yours does, you should consider a more React-centric approach to manipulating styles and classes. Avoid direct DOM manipulations with document.querySelector and instead use React state to conditionally apply styles or classes. Here's how you can apply this to your show handlers:
Use State to Control Visibility and Classes: Instead of directly manipulating the DOM to show or hide content, use React state to hold visibility flags or class names and conditionally apply them.
Modify your component to include state for managing visibility and/or class names:
jsx
import React, { useState } from 'react';
import "../styles/Body.css";
import { store } from '../productsStore/Store';
import HoverImage from "react-hover-image/build";
import { Link } from 'react-router-dom';
const Body = () => {
const [selectedCategory, setSelectedCategory] = useState("");
const contentOpacity = (category) => {
return selectedCategory === category ? "1" : "0";
}
const contentClass = (category) => {
const baseClass = `${category}Content transition`;
// Assuming transition classes are named e.g., transition, transition2 for simplicity
return selectedCategory === category ? baseClass + selectedCategory.slice(-1) : baseClass;
}
const showHandler = (category) => () => {
setSelectedCategory(category);
}
const dontShowHandler = () => {
setSelectedCategory("");
}
// Your component JSX goes here, using contentOpacity and contentClass functions
// to dynamically set the styles and classes based on component state.
}
Dynamically Apply Styles and Classes: In your component's JSX, use the style attribute and className to apply the opacity and any additional classes based on the component's state. This replaces direct DOM manipulations and integrates better with React's rendering lifecycle.
jsx
<div className={contentClass('body')} style={{opacity: contentOpacity('body')}} onMouseLeave={dontShowHandler}>
{/* Body category content */}
</div>
<div className={contentClass('hair')} style={{opacity: contentOpacity('hair')}} onMouseLeave={dontShowHandler}>
{/* Hair category content */}
</div>
{/* Repeat for other categories */}
Event Handlers: Pass the showHandler with the appropriate category as an argument to the onMouseMove (or any event handler) of the element that triggers the change.
jsx
<div className='bodyHold' onMouseMove={showHandler('body')}>
<p> BODY </p>
</div>
This approach leverages React's state management and rendering lifecycle to conditionally render styles and classes, which is a more natural fit for React applications and avoids direct DOM manipulation errors.