javascript - React 组件不在 onClick 调用的函数中执行
问题描述
首先,我从 App 调用 Country 组件,传递 showCountry(CountryDetails 不会执行的地方)
const showCountry = (country) => {
console.log(country)
return (
<CountryDetails country={country} />
)
}
...
<Countries countries={countries} filter={filter} handleClick={showCountry}/>
国家/地区列表,每个国家/地区旁边都有一个按钮以显示国家/地区详细信息
<ul>
{toShow.map(country => <li>{country.name} <Button country={country} handleClick={handleClick}/></li>)}
</ul>
我的按钮组件,在尝试将国家/地区数据传递给事件处理程序时遇到了一些麻烦。结束了下面的解决方案(不知道这是一个好习惯)
const Button = ({ handleClick, country}) => (
<button onClick={() => handleClick(country)}>
show
</button>
)
const CountryDetails = props => {
const country = props.country
console.log('country details..')
return ...
}
函数 showCountry 使用传递给它的正确数据执行,但不执行 CountryDetails。
很抱歉发了很长的帖子,欢迎任何关于更好做法的反馈!
解决方案
推荐阅读
- html - 下载文件时边框和背景移动
- javascript - 使用 vanilla javascript 将单个子元素插入/附加到多个父元素
- pycharm - pycharm问题非项目文件
- react-native-android - react-native android:可能的未处理承诺拒绝(id:0):TypeError:网络请求失败
- html - 带有背景图像的标签
- python - 在 python 中使用 ast 从字符串表达式中排序变量名时出错
- python - 覆盖面板布局组件?
- visual-studio-code - 写一个 vscode 扩展可视化测试
- ios - 我尝试注册 BGTaskScheduler.shared.register 但返回错误
- javascript - WebGL透视投影矩阵不起作用