gpt4 book ai didi

reactjs - React router 4 `Link`组件仅更改url而不更新路由

转载 作者:行者123 更新时间:2023-12-03 13:37:45 25 4
gpt4 key购买 nike

我在使用react-router-dom Link时遇到问题组件仅更改 URL,不更新路由。

它可以很好地链接 /courses -> /courses/<course-slug>
然后/courses/<course-slug> -> /lessons/<lesson-slug>

但是,我在链接到其他类(class)时遇到问题,例如来自/lessons/<lesson-slug> -> /lessons/<different-lesson-slug>

看起来它只更新了 LessonsList组件并更新 URL,但不更新路由/内容或父 Lesson .

我已确保将匹配、位置属性传递给组件,以防它与更新阻止有关 - https://reacttraining.com/react-router/web/guides/dealing-with-update-blocking但似乎仍然不起作用。

我看不出哪里出错了,而且应该相对简单。

这与我设置路线或具有相同路线的路线有什么关系吗?

这是我的依赖项和代码,任何正确方向的点都将受到赞赏。

"dependencies": {
"axios": "^0.16.2",
"prop-types": "^15.5.10",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-router-dom": "^4.1.2"
}

index.js

    import css from '../css/index.scss';
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router } from 'react-router-dom';
import App from './components/App';

ReactDOM.render(
<Router>
<App />
</Router>
, document.getElementById('app'));

应用程序/index.js

import React, { Component } from 'react';
import Header from '../Header';
import Main from '../Main';
import Footer from '../Footer';

class App extends Component {
render() {
return(
<div>
<Header />
<Main />
<Footer />
</div>
);
}
}

export default App;

主/index.js

import React, { Component } from 'react';
import { Route, Link, Switch } from 'react-router-dom';
import Home from '../Home';
import Courses from '../Courses';
import Course from '../Course';
import Lessons from '../Lessons';
import Lesson from '../Lesson';

class Main extends Component {
constructor(props) {
super(props);
}

render() {
return(
<div className="main">
<Switch>
<Route exact path="/" component={Home}/>
<Route path="/courses/:course" component={Course}/>
<Route exact path="/courses" component={Courses}/>
<Route path="/lessons/:lesson" component={Lesson}/>
<Route exact path="/lessons" component={Lessons}/>
<Route render={ () => (
<div>Not Found 404</div>
)}/>
</Switch>
</div>
);
}
}

export default Main;

类(class)/index.js

import React, { Component } from 'react';
import api from '../../utils/api';
import LessonsList from '../LessonsList';
import { Link } from 'react-router-dom';

class Lessons extends Component {
constructor(props) {
super(props);

this.state = {
lessons: null
};
}

componentDidMount() {
api.getAllLessons()
.then((lessons) => {
this.setState({
lessons: lessons
});
});
}

render() {
return(
<div className="lessons">
{!this.state.lessons ?
<div>Loading...</div>
:
<div>
<LessonsList
lessons={this.state.lessons}
{...this.props}
/>
</div>
}
</div>
);
}
}

export default Lessons;

类(class)/index.js

import React, { Component } from 'react';
import api from '../../utils/api';
import LessonsList from '../LessonsList';
import { Link } from 'react-router-dom';

class Lesson extends Component {
constructor(props) {
super(props);

this.state = {
lesson: null
}
}

componentDidMount() {
api.getLesson(this.props.match.params.lesson)
.then((lesson) => {
this.setState({
lesson: lesson[0]
});
});
}

render() {
return(
<div className="lesson">
{!this.state.lesson ?
<div>Loading...</div>
:
<div>
<h3>Course title: {this.state.lesson.course.title}</h3>
<h1>Lesson: {this.state.lesson.title}</h1>
<h2>Other lessons from this course</h2>
<LessonsList
lessons={this.state.lesson.lessons}
{...this.props}
/>
</div>
}
</div>
);
}
}

export default Lesson;

LessonsList/index.js

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';

function LessonsList(props) {
return(
<ul>
{props.lessons.map((lesson) => {
return(
<li key={lesson.id}>
<Link to={`/lessons/${lesson.slug}`}>{lesson.title}</Link>
</li>
);
})}
</ul>
);
}

LessonsList.propTypes = {
lessons: PropTypes.array.isRequired
}

export default LessonsList;

更新:

这是更新后的组件 componentWillReceiveProps

类(class)/index.js

import React, { Component } from 'react';
import api from '../../utils/api';
import LessonsList from '../LessonsList';
import { Link } from 'react-router-dom';

class Lesson extends Component {
constructor(props) {
super(props);

this.state = {
lesson: null
}
}

componentDidMount() {
api.getLesson(this.props.match.params.lesson)
.then((lesson) => {
this.setState({
lesson: lesson[0]
});
});
}

componentWillReceiveProps(nextProps) {
if(this.props.match.params.lesson !== nextProps.match.params.lesson) {
api.getLesson(nextProps.match.params.lesson)
.then((lesson) => {
this.setState({
lesson: lesson[0]
});
});
}
}

render() {
return(
<div className="lesson">
{!this.state.lesson ?
<div>Loading...</div>
:
<div>
<h3>Course title: {this.state.lesson.course.title}</h3>
<h1>Lesson: {this.state.lesson.title}</h1>
<h2>Other lessons from this course</h2>
<LessonsList
lessons={this.state.lesson.lessons}
{...this.props}
/>
</div>
}
</div>
);
}
}

export default Lesson;

最佳答案

您的<Lesson />组件仅在 componentDidMount 期间设置类(class)生命周期 Hook 。如果您在上课时更换了 slug,则不会导致组件重新安装。您可以使用 componentWillReceiveProps生命周期 Hook 来完成您想要的事情。

componentWillReceiveProps(nextProps) {
api.getLesson(nextProps.match.params.lesson)
.then((lesson) => {
this.setState({
lesson: lesson[0]
});
});
}

关于reactjs - React router 4 `Link`组件仅更改url而不更新路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45510784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com