reactjs - React.js 如何将数据从渲染传递到方法
问题描述
我想通过路由链接将数据从组件 A 传递到组件 B,然后在组件 B 中发出 API 请求,我能够将它从组件 A 传递到 B,但无法弄清楚如何从内部渲染传递该数据到将发出 API 请求的方法。希望我很清楚请看下面的代码。并提前感谢。
组分 A
<ul>
{this.state.movies.map(movie => (
<li key={movie.imdbID}>
<img alt="img" src={movie.Poster} />
<h1>{movie.Title}</h1>
<p>{movie.Year}</p>
<button>
<Link to={{ pathname: "./productdetail", movieid: movie.imdbID }}>View More</Link></button>
</li>))}
</ul>
B组份
class ProductDetailPage extends React.Component {
state = {
movieIdSearch: []
};
movieIdRequest(id) {
axios.get(`http://www.omdbapi.com/?apikey=bcfe7e46&i=${id}`).then(res => {
const movieById = res.data;
this.setState({ movieIdSearch: movieById });
});
}
render() {
const {
Poster,
Title,
Year,
Released,
Runtime,
Genre,
Country,
Language,
Actors,
Plot
} = this.state.movieIdSearch;
return (
<div>
{/*how to pass this.props.location.movieid to a movieIdRequest method*/}
<img alt="img" src={Poster} />
<h3>{Title}</h3>
<div>
<p>{Year}</p>
<p>{Released}</p>
<p>{Runtime}</p>
<p>{Genre}</p>
<p>{Country}</p>
<p>{Language}</p>
</div>
<div>
<h5>{Actors}</h5>
<p>{Plot}</p>
</div>
</div>
);
}
}
解决方案
推荐阅读
- r - 改进 R 中的缺失值删除代码
- javascript - 如何加载从文本框中选择的特定年份的完整日历?
- openbsd - OpenSMTPD 邮件不会从客户端发送,原因=ca-failure
- node.js - 从天气 API 获取数据到 Twitter 机器人
- spring-boot - 如何知道使用 HttpServletRequest 或 HttpServletResponse 对象调用了哪个 Rest 控制器方法?
- java - 如何生成一个方法来接受具有相同方法的注释?
- python - 从 API 返回的列表中获取值
- airflow - 如果任务在气流中卡在排队状态怎么办?如何排除故障?
- javascript - 使用javascript自动计算具有来自mysql的数据的表行
- java - 获取 div 类中的元素列表