Carlos Junior
Carlos Junior

Reputation: 1

How to update component that is being rendered on route of 'react-router-dom'?

When my App runs componentDidMount() I fetch the data

async componentDidMount() {
  try {
    let players = await playersDAO.read();
    this.setState({ players: players });
  } catch (e) {
    console.log(e);
  }
}

Then show it on a component that is being rendered by Route from react-router-dom

<BrowserRouter>
  <Switch>
    <Route
      key="players"
      path="/players"
      render={() => <Players rows={this.state.players} />}
    />
  </Switch>
</BrowserRouter>

I don't know why, but when the data is fetched the component do not update. Maybe(there's a large chance) I'm doing something wrong, but can anyone help me with it?

Off-topic: I use the render propriety of Route, because i need to pass props for my component, anyone knows a better way of doing so?

Upvotes: 0

Views: 1893

Answers (1)

Bobyd
Bobyd

Reputation: 1

You can call the route after setting state using history.push('/players').

Upvotes: 0

Related Questions