我希望能够通过每个map函数传递一个名为
我的输出为空白。不应该可以工作吗?
更新代码:
verified
的prop,但我在设置它时遇到了困难。
更新:
将verified
传递给renderContinents可以工作,但是当像这样向renderCountry添加参数时: {continent.countries.map(renderCountry(null, verified))}
我的输出为空白。不应该可以工作吗?
更新代码:
const renderCities = cities => {
return (
<div>
<label>
<input
onChange={onChange}
type="checkbox"/>
{cities.name}
</label>
</div>
);
};
const renderCountries = ({country, verified}) => {
console.log("came to country");
return (
<div className="city-location">
<label>
<input
onChange={onChange}
type="checkbox"/>
{country.name}
</label>
{country.cities.map(renderCities)}
</div>
);
};
function onChange(e) {
console.log('checkbox verified:', (e.target.verified));
}
class AreasComponent extends Component {
constructor(props) {
super(props);
this.state = {
};
this.renderContinents = this.renderContinents.bind(this);
}
componentWillMount() {
this.props.fetchAllAreas();
}
renderContinents(verified, continent) {
console.log("came to continent");
return(
<div className="continent-location">
<label>
<input
onChange={onChange}
type="checkbox"/>
{continent.name}
</label>
{continent.countries.map(renderCountries(null, verified))}
</div>
)
}
render() {
if (!this.props.verified || !this.props.areas) {
return <div>Loading Areas...</div>
}
return(
<div>
{this.props.areas.map(this.renderContinents.bind(this, this.props.verified))}
</div>
);
}
}
function mapDispatchToProps(dispatch){
return bindActionCreators({ fetchAllAreas, checkArea}, dispatch);
}
function mapStateToProps(state) {
return { areas: state.areas.all,
verified:state.areas.verified
};
}
export default connect(mapStateToProps, mapDispatchToProps)(AreasComponent);
我的另一个问题是onChange(e)
函数。它是全局的,所以当我点击任何复选框时都会起作用,但我想让它能够在被点击时接受一个参数并分派checkArea
操作,这对我来说意味着它必须被绑定并且也应该作为参数传递。我尝试过这样做:
{this.props.areas.map(this.renderContinents.bind(this, this.props.verified, this.props.checkArea))}
但它返回一个空结果。是否可以将函数发送到map()参数中,是否有办法使renderCountry/renderCity与参数一起工作?