我面临一个非常奇怪的问题,导致感觉像“无论你怎么样”
首先是代码:
renderImages = () => {
let i = 0;
const arr = [
"https://truffle-assets.imgix.net/pxqrocxwsjcc_6OcJeUMaWIYOmKgsK6IwWc_galaxy-cupcakes_squareThumbnail_en-US.jpeg",
"https://sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-5.jpg",
"http://is4.mzstatic.com/image/thumb/Purple127/v4/74/47/31/7447317e-9844-3ee0-d189-661a55c94abe/source/1200x630bb.jpg",
"https://i.pinimg.com/736x/55/03/94/550394c428e268868aa73e509302b84c--neopolitan-cupcakes-ice-cream-cupcakes.jpg"
];
return arr.map((item) => {
i++;
return(
<div>
<img src={item} onClick={() => alert(i)} alt={i} />
</div>
);
})
};
现在当我点击图片时,它总是显示4.第一张图片显示4张图片和最后一张图片显示4.所有人都提醒4.但是,他们的alt标签似乎是正确的。我不认为我做错了什么,但我是:)
我的反应版本是16.1.1
所以任何人都知道我在这里做错了什么?我不认为这是一个绑定问题,但谁知道。
答案 0 :(得分:5)
JavaScript的范围是函数级别,而不是块级别,创建闭包只意味着封闭范围被添加到封闭函数的词法环境中。
循环终止后,函数级变量i的值为4,内部函数&#39;看到的是什么。
现在即使let
有一个块范围,它的范围也只限于renderImages
函数而不是map
,因此你仍然有一个闭包问题。
但是map
为second argument
提供了index
,您可以使用它来代替递增变量并使用它
像
renderImages = () => {
const arr = [
"https://truffle-assets.imgix.net/pxqrocxwsjcc_6OcJeUMaWIYOmKgsK6IwWc_galaxy-cupcakes_squareThumbnail_en-US.jpeg",
"https://sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-5.jpg",
"http://is4.mzstatic.com/image/thumb/Purple127/v4/74/47/31/7447317e-9844-3ee0-d189-661a55c94abe/source/1200x630bb.jpg",
"https://i.pinimg.com/736x/55/03/94/550394c428e268868aa73e509302b84c--neopolitan-cupcakes-ice-cream-cupcakes.jpg"
];
return arr.map((item, index) => {
return(
<div>
<img src={item} onClick={() => alert(index)} alt={index} />
</div>
);
})
};
以下是如何为嵌套数组执行此操作
const arr = [[
"https://truffle-assets.imgix.net/pxqrocxwsjcc_6OcJeUMaWIYOmKgsK6IwWc_galaxy-cupcakes_squareThumbnail_en-US.jpeg",
"https://sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-5.jpg",
"http://is4.mzstatic.com/image/thumb/Purple127/v4/74/47/31/7447317e-9844-3ee0-d189-661a55c94abe/source/1200x630bb.jpg",
"https://i.pinimg.com/736x/55/03/94/550394c428e268868aa73e509302b84c--neopolitan-cupcakes-ice-cream-cupcakes.jpg"
],[
"https://truffle-assets.imgix.net/pxqrocxwsjcc_6OcJeUMaWIYOmKgsK6IwWc_galaxy-cupcakes_squareThumbnail_en-US.jpeg",
"https://sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-5.jpg",
"http://is4.mzstatic.com/image/thumb/Purple127/v4/74/47/31/7447317e-9844-3ee0-d189-661a55c94abe/source/1200x630bb.jpg",
"https://i.pinimg.com/736x/55/03/94/550394c428e268868aa73e509302b84c--neopolitan-cupcakes-ice-cream-cupcakes.jpg"
]];
const renderImages = (arr, idx) => {
return arr.map((item, index) => {
return(
<div>
<img src={item} onClick={() => alert(idx + index)} alt={idx + index} />
</div>
);
})
};
const App = () => {
let i = 0;
return (
<div>{arr.map((array, index) => {
const val = renderImages(array, i);
i += array.length;
return <div>{val}</div>
})}</div>
)
}
ReactDOM.render(<App/>, document.getElementById('app'));
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"/>
&#13;
答案 1 :(得分:0)
尝试按以下方式修改代码:
return arr.map((item, index) => {
return(
<div>
<img src={item} onClick={() => alert(index)} alt={index} />
</div>
);
})
这样你可以完全删除i
。另请注意,索引是从零开始的,因此您可能希望将其增加为1。