【发布时间】:2020-09-10 04:59:18
【问题描述】:
我学习反应并停留在这个地方。我正在创建一个应用程序,用户将在其中看到具有不同 ID 和名称的产品列表。我创建了另一个组件,产品的详细信息将在其中打开。我通过 onClick 函数在我的 addList 组件中收集特定产品的 id 和 value。现在我想在 DetailList 组件中发送这些值,以便我可以显示该特定产品的详细信息。
类似的路线图
添加列表->(用户单击产品)->产品的ID和名称传递给DetailList组件->通过获取产品详细信息打开详细列表组件。
这是我添加列表组件的代码
export default class Addlist extends Component {
constructor(props) {
super(props)
this.state = {
posts : []
}
}
passToDetailList(id) {
console.log( id)
}
async componentDidMount() {
axios.get('http://localhost:80/get_add_list.php')
.then(response => {
console.log(response);
this.setState({posts: response.data})
})
.catch(error => {
console.log(error);
})
}
render() {
const { posts } = this.state;
// JSON.parse(posts)
return (
<Fragment>
<div className="container" id="listOfAdd">
<ul className="addUl">
{
posts.map(post => {
return (
<li key={post.id}>
<div className="row">
<div className="col-md-4">
<img src={trialImage} alt=""></img>
</div> {/* min col end */}
<div className="col-md-8">
<b><h2>{post.add_title}</h2></b>
{/* This button is clicked by user to view detail of that particular product */}
<button onClick={() => this.passToDetailList(post.id)}>VIEW</button>
</div> {/* min col end */}
</div> {/* row end */}
</li>
);
})}
</ul>
</div>{/* container end */}
</Fragment>
)
}
}
【问题讨论】:
标签: javascript reactjs react-router jsx