this.props.history.push(..)
を使用してプログラムでルーティングしようとしていますが、動作しないようです。React-router v4 this.props.history.push(...)not working
がここにルータです:CUSTOMERLISTで
import {
BrowserRouter as Router,
Route
} from 'react-router-dom';
<Router>
<Route path="/customers/" exact component={CustomersList} />
<Route path="/customers/:id" exact component="{Customer} />
</Router>
、顧客のリストがレンダリングされます。顧客(li)をクリックすると、顧客への申請ルートを作成する必要があります。
import { withRouter } from 'react-router'
class Customers extends Component {
static propTypes = {
history: PropTypes.object.isRequired
}
handleCustomerClick(customer) {
this.props.history.push(`/customers/${customer.id}`);
}
render() {
return(
<ul>
{ this.props.customers.map((c) =>
<li onClick={() => this.handleCustomerClick(c)} key={c.id}>
{c.name}
</li>
</ul>
)
}
}
//connect to redux to get customers
CustomersList = withRouter(CustomersList);
export default CustomersList;
コードは部分的ですが完全な状況を示しています。 ブラウザのアドレスバーがhistory.push(..)に応じて変更されますが、ビューは更新されず、Customerコンポーネントはレンダリングされず、CustomersListはまだそこにあります。何か案は?
こんにちは、実際に起こっていることは、両方のコンポーネントが一致し、両方のコンポーネントがレンダリングされているということです。両方のコンポーネントがそれぞれの 'componentDidMount'を実行しているかどうか確認できますか? –
@FacundoLaRocca彼らは両方のルートに 'exact'を持っています – Li357
はい、私は知っています、しかし私は同様の問題に直面しました。それが私が求めている理由です。 –