我在react中编写了一个带有自动建议的简单搜索组件,它调用themoviedb。我正在使用react router dom,并在app.js中定义了如下路由参数:
<Route path="/:id" component={SomeComponent} />
搜索组件如下所示:
import React, { Component, Fragment } from "react";
import { Link } from "react-router-dom";
import styled from "styled-components";
import axios from "axios";
const SuggestionsResult = styled.ul`
text-decoration: none;
list-style: none;
text-align: left;
display: flex;
flex-direction: column;
width: 100%;
`;
const ResultItem = styled.li`
border-bottom: 0.0625rem solid hsla(0, 0%, 100%, 0.12);
padding: 10px 0 10px;
padding-left: 2px;
font-size: 1em;
cursor: pointer;
&:hover {
background: hsla(0, 0%, 100%, 0.12);
}
`;
export default class Search extends Component {
state = {
query: "",
results: [],
showSuggestions: false
};
handleInputChange = () => {
this.setState(
{
query: this.search.value
},
() => {
if (this.state.query && this.state.query.length > 1) {
if (this.state.query.length % 2 === 0) {
axios
.get(
`https://api.themoviedb.org/3/search/movie?api_key=${apiKey}&language=en-US&query=${
this.state.query
}&page=1&include_adult=false`
)
.then(({ data }) => {
this.setState({
results: data.results,
showSuggestions: !this.state.showSuggestions
});
});
}
} else if (!this.state.query) {
}
}
);
};
handleSuggestionClick = e => {
this.setState({ showSuggestions: false });
};
render() {
return (
<Fragment>
<input
placeholder="Search for a movie..."
ref={input => (this.search = input)}
onChange={this.handleInputChange}
/>
{this.state.showSuggestions && (
<Suggestions
results={this.state.results}
handleSuggestionClick={this.handleSuggestionClick}
/>
)}
</Fragment>
);
}
}
const Suggestions = ({ results, handleSuggestionClick }) => {
const options = results.map(r => (
<ResultItem key={r.id}>
<Link onClick={handleSuggestionClick} to={`/${r.id}`}>
{r.title}
</Link>
</ResultItem>
));
return <SuggestionsResult>{options}</SuggestionsResult>;
};
我的问题是,当点击链接时,它会改变url,但它会停留在同一个站点上。如果我不使用react路由器dom链接组件,只使用元素,它可以正常工作,但所有东西都会重新渲染。
**在app.js中更新我的react路由器代码
<Router>
<Switch>
<Route exact path="/" component={MoviesList} />
<Route path="/:id" component={MovieDetail} />
</Switch>
</Router>
“建议”组件未收到路由器道具,因此导致此问题。将您的建议组件包装为withRouter
HOC。还要确保搜索组件呈现为子组件或路由器
组件
import React, { Component, Fragment } from "react";
import { Link, withRouter } from "react-router-dom";
import styled from "styled-components";
import axios from "axios";
const SuggestionsResult = styled.ul`
text-decoration: none;
list-style: none;
text-align: left;
display: flex;
flex-direction: column;
width: 100%;
`;
const ResultItem = styled.li`
border-bottom: 0.0625rem solid hsla(0, 0%, 100%, 0.12);
padding: 10px 0 10px;
padding-left: 2px;
font-size: 1em;
cursor: pointer;
&:hover {
background: hsla(0, 0%, 100%, 0.12);
}
`;
export default class Search extends Component {
state = {
query: "",
results: [],
showSuggestions: false
};
handleInputChange = () => {
this.setState(
{
query: this.search.value
},
() => {
if (this.state.query && this.state.query.length > 1) {
if (this.state.query.length % 2 === 0) {
axios
.get(
`https://api.themoviedb.org/3/search/movie?api_key=${apiKey}&language=en-US&query=${
this.state.query
}&page=1&include_adult=false`
)
.then(({ data }) => {
this.setState({
results: data.results,
showSuggestions: !this.state.showSuggestions
});
});
}
} else if (!this.state.query) {
}
}
);
};
handleSuggestionClick = e => {
this.setState({ showSuggestions: false });
};
render() {
return (
<Fragment>
<input
placeholder="Search for a movie..."
ref={input => (this.search = input)}
onChange={this.handleInputChange}
/>
{this.state.showSuggestions && (
<Suggestions
results={this.state.results}
handleSuggestionClick={this.handleSuggestionClick}
/>
)}
</Fragment>
);
}
}
const Suggestions = withRouter(({ results, handleSuggestionClick }) => {
const options = results.map(r => (
<ResultItem key={r.id}>
<Link onClick={handleSuggestionClick} to={`/${r.id}`}>
{r.title}
</Link>
</ResultItem>
));
return <SuggestionsResult>{options}</SuggestionsResult>;
});
在我的存储区被调度之后,我调用,它将我发送到正确的视图,但我无法访问查询参数。我的其余代码可以在https://github.com/crh225/hotreactasp/tree/master/src/hotreactasp/content找到,谢谢!
什么似乎是简单的集成与react-router-dom有意想不到的结果。当我点击侧边栏中的链接时,链接组件通过生成网址(即localhost:3000/about)正常工作,但我仍然停留在以前的查看页面上。当我点击前进和后退按钮时,路由会呈现正确的组件。 我正在使用Webpack和Typescript,但我不认为它们会影响任何事情。 “反应”:“^16.12.0”, "report-router-
问题内容: 我想用可选的path参数声明一个路径,因此当我添加它时,页面会做一些额外的事情(例如,填充一些数据): http:// localhost / app / path / to / page <=渲染页面 http:// localhost / app / path / to / page / pathParam <=根据pathParam使用某些数据渲染页面 在我的React Rout
我的目标是让http://mydomain/route1呈现React组件Component1,让http://mydomain/route2呈现component2。因此,我认为编写如下路线是很自然的: http://mydomain/route1按预期工作,但http://mydomain/route2反而呈现Component1。 然后我读了这个问题,并将路线改为:
我在使用react路由器引导时遇到活动链接问题(https://www.npmjs.com/package/react-router-bootstrap)组成部分。当我导航到“关于”时,活动类不仅出现在“关于”上,而且也出现在“主页”上。 主动链路路由器的问题 这是我的Nav组件从react-bootstrap(https://react-bootstrap.github.io/): NavLin