0

I need bind states between two components (a navbar an a map). Both components are receiving data from index state via props.

How can I update index state in navbar component in order to update map component?

In index.ts init

this.state: { routes: {geojson_routes}, loadaded: false }

In navbar.tsx

const [routes, setRoutes] = React.useState(props.routes)
const [loaded, setLoaled] = React.useState(props.loaded)

const showRoute= () => {
    setLoaled(true)
}

In map.tsx

if(props.loaded == true) show = geojason(props)

I guess what I need is when showRoute is call from navbar, update loaded value from index.ts... But I don't know how ¬¬'

MAL
  • 143
  • 1
  • 14

1 Answers1

1

You can pass a function that sets the loaded state to true to the Navbar component as a prop. Then, when you want to set loaded to true, call the function from the navbar component.

Nils Fahle
  • 126
  • 1
  • 8
  • Thank you for your answer! Now I have state like "this.state = {routes="geojson", loaded: false, load: () => this.state.loaded = true};" passed as props to the Navbar component. I can call it from there, but it does not execute the updateView. – MAL Jun 02 '21 at 11:07