【发布时间】:2019-12-26 23:12:45
【问题描述】:
我正在使用 React 创建一个应用程序,我可以在其中查看有关每个国家/地区的标志和各种信息。我正在使用 API 来获取数据,并且我已经用网格将它们全部映射了。到目前为止,这是我的代码:
class App extends React.Component{
constructor (props){
super (props);
this.state={
countries : [],
info: ""
}
}
componentDidMount(){
axios.get(`https://restcountries.eu/rest/v2/all`)
.then(res => {
const data = res.data;
console.log(data);
this.setState({
countries : data
})
this.showInfo = this.showInfo.bind(this)
})
}
showInfo (e) {
console.log(e.target.key);
}
render() {
return (
<div className="container">
{this.state.countries.map(country=>
<Country name={country.name}
key={country.name}
population ={country.population}
region={country.region}
capital={country.capital}
flag={country.flag}
showInfo={this.showInfo}
/>
)}
</div>
)
}
}
export default App;
这是我的 Country-item 组件:
const Country = ({name, population, region, capital, flag, showInfo})=>{
return (
<div onClick={showInfo} className="country-item">
<div className="img">
<img src={flag}/>
</div>
<p>{name}</p>
<p>population: {population}</p>
<p>Region: {region}</p>
<p>Capital: {capital}</p>
</div>
)
}
export default Country
到目前为止,我有这样的事情: enter image description here
现在我想单击每个国家/地区框项目并在模式弹出窗口中显示单击的数据。如果我创建一个模态并且我将映射它,当然在单击时我会将它们全部显示一次。我如何传递我单击模态组件的那个框的道具?我创建了一个函数试图捕获例如关键道具,但我没有成功。最好的策略是什么?非常感谢您的帮助
【问题讨论】:
标签: javascript reactjs