score:2

you are causing continuous renders by modifying the state in the render() function.

instead i would do the following - not using for ismain state variable:

render() {
    let ismain = false;

    if (window.location.pathname === '/') {
        ismain = true;
    } else if (window.location.pathname === '/ourworks') {
        ismain = false;
    }

    return (
        <div>
            <headercommon ismain={ismain} />
            <route path='/' exact component={index} />
            <route path='/ourworks' exact component={gallery} />
            <footercommon />
        </div>
    );
}

or even shorter:

render() {
    const ismain = window.location.pathname === '/';

    return <div>
        <headercommon ismain={ismain} />
        <route path='/' exact component={index} />
        <route path='/ourworks' exact component={gallery} />
        <footercommon />
    </div>
}

i hope this helps!

score:3

a react hookful solution

an alternative to the class-based solution is involving react's new addition of hooks.

react hooks were introduced in react v16.8 and highly reduce the boilerplate your code requires to initialize and render components.

if you're interested i highly recommend exploring the react hook documentation here.

code

import react, { component, usestate, useeffect } from "react";
import index from './index/layout/layout';
import gallery from './gallery/layout/layout';
import headercommon from './common/header/header';
import footercommon from './common/footer/footer';
import { route } from 'react-router-dom';

export const path = (props) => {
    const [ismain, setismain] = usestate(null);

    useeffect(() => {
        setismain(window.location.pathname === '/')
    }, []);

    return (
        <div>
            <headercommon ismain={ismain} />
            <route path='/' exact component={index} />
            <route path='/ourworks' exact component={gallery} />
            <footercommon />
        </div>
    );
};

the usestate hook

the usestate hook is common-place in react functional components and is imported directly from react with import {usestate} from "react.

we simply provide a default value in the function expression, e.g. usestate(null) which will set out initial state to a value of null. we can set this value to be any value, a data structure or an object.

we assign a way to access and set the state with array destructuring:

const [ismain, setismain] = usestate(null);

we can now access our state with calls to ismain or set the state with setismain.

the useeffect hook

the useeffect hook is similar to componentdidmount and componentdidupdate lifecycle methods in class-based react. this hook will automatically call depending on the second argument you pass to this hook. if no second argument is supplied, the hook will automatically act as componentdidupdate.

in the solution above, i pass an empty array ([]) as a second parameter to the useeffect hook callback. this means that the state value only updates on the components initial render and is effectively equivalent to componentdidmount.

useeffect(() => {
    setismain(window.location.pathname === '/')
}, []);

additonally, we can pass values into this array to tell react when the component should re-render, but that's outside the scope of this answer.

again, much more details about the usestate hook, useeffect hook, and react hooks in general can be found in the docs.


Related Query

More Query from same tag