React-router-dom how to update parent state inside route component

Microos

I have a class parent class App containing a navbar and a bunch of routes:

function App() {
    const [showNavbar, setShowNavbar] = useState(true);

    return (
        <>

            <MyNavBar show={showNavbar}/>
            <Switch>
                <Route exact path='/child' component={() => <ChildPart setter={showNavbar}/>}/>
                <Route> ... //other routes
            </Switch>
        </>
    )
}
function ChildPart(props) {
    const [data, setData] = useState("");

    // hide navbar
    useEffect(() => {
        console.log("child init"); // this got printed twice


        props.setter(false);
    }, [props.setter]);

    // get data at first mount;
    useEffect(()=>{
        setData(api.loadUserInfo());

    }, []);

    return (<div>
        Data here: {data}
    </div>)
}

What I want to achieve is to have navbar change based on which page a user is currently at. For example, hide navbar when use is visiting /child

But by implementing it this way, the ChildPart seems got mount and unmount for twice since the parent is rendered and rotues are also a part of the App.

This causes unnecessary API calls inside the child component.

Is there any way to address this?

Danko

If hiding one (MyNavBar) component by navigating to one (or more) urls is a goal, something like this could work. Unfortunatelly negative lookup (regex) is not supported by react router so we can't use something like this (?!child).

Another thing which you can do is making MyNavBar aware of current url by wrapping it withRouter HOC or using useLocation hook. Than, you can do your logic in MyNavBar depending on url.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can I pass the component's name from another component's object to Route in React Router Dom?

分類Dev

React update child state from parent component

分類Dev

How do I transmit a state to a parent component when clicking a react-router Link?

分類Dev

React router dom passing data from parent component to child router component does not pass the props.match

分類Dev

How to call a component inside a component using React Router

分類Dev

How to update parent state array from child component?

分類Dev

React Router Switch statement with routes grouped as component inside does not go to Not Found route

分類Dev

React Router Dom Link in another component

分類Dev

React-Router - Route re-rendering component on route change

分類Dev

Does state update of parent component assures remount of child component in ReactJS?

分類Dev

Angular UI Router: How to save parent state?

分類Dev

React component not updating on redux state update

分類Dev

React component does not update with the change in redux state

分類Dev

React-Router: How do I add a new component and route to the onboarding steps on a wizard?

分類Dev

How to avoid unnecesary update in a stateless component with a React class-based component as a parent

分類Dev

How to create a dynamic routing naming the routes based on props of a component in react-router-dom

分類Dev

How do I sync state from the DOM to my React Component in an Isomorphic Application?

分類Dev

State doesnt get reset on route change - React-redux-router

分類Dev

How to test a component with the <Router> tag inside of it?

分類Dev

react component connected, redux state changes... but no update to component?

分類Dev

React. How to update the state with previous state?

分類Dev

react router dom is not updating class component when url change

分類Dev

Why is state undefined when passed through React Router Link Component?

分類Dev

how to remove the warning "can't perform a react state update on unMounted Component"

分類Dev

React, get bound parent dom element name within component

分類Dev

How to Use nested routes in react router dom

分類Dev

How to update context state inside componentDidMount?

分類Dev

React: How to add onChange functionality inside of Function component using React Hooks? Need onClick event from a checkbox to influence input state

分類Dev

React not re-rending DOM after state update

Related 関連記事

  1. 1

    How can I pass the component's name from another component's object to Route in React Router Dom?

  2. 2

    React update child state from parent component

  3. 3

    How do I transmit a state to a parent component when clicking a react-router Link?

  4. 4

    React router dom passing data from parent component to child router component does not pass the props.match

  5. 5

    How to call a component inside a component using React Router

  6. 6

    How to update parent state array from child component?

  7. 7

    React Router Switch statement with routes grouped as component inside does not go to Not Found route

  8. 8

    React Router Dom Link in another component

  9. 9

    React-Router - Route re-rendering component on route change

  10. 10

    Does state update of parent component assures remount of child component in ReactJS?

  11. 11

    Angular UI Router: How to save parent state?

  12. 12

    React component not updating on redux state update

  13. 13

    React component does not update with the change in redux state

  14. 14

    React-Router: How do I add a new component and route to the onboarding steps on a wizard?

  15. 15

    How to avoid unnecesary update in a stateless component with a React class-based component as a parent

  16. 16

    How to create a dynamic routing naming the routes based on props of a component in react-router-dom

  17. 17

    How do I sync state from the DOM to my React Component in an Isomorphic Application?

  18. 18

    State doesnt get reset on route change - React-redux-router

  19. 19

    How to test a component with the <Router> tag inside of it?

  20. 20

    react component connected, redux state changes... but no update to component?

  21. 21

    React. How to update the state with previous state?

  22. 22

    react router dom is not updating class component when url change

  23. 23

    Why is state undefined when passed through React Router Link Component?

  24. 24

    how to remove the warning "can't perform a react state update on unMounted Component"

  25. 25

    React, get bound parent dom element name within component

  26. 26

    How to Use nested routes in react router dom

  27. 27

    How to update context state inside componentDidMount?

  28. 28

    React: How to add onChange functionality inside of Function component using React Hooks? Need onClick event from a checkbox to influence input state

  29. 29

    React not re-rending DOM after state update

ホットタグ

アーカイブ