快速填充元素?

React 中有没有快速创建元素技巧?
比如像Vue这样?

<span v-for="n in 10">{{ n }} </span>

我能想到React中最快的方式

{[...Array(10)].map((n, i) => (
  <Span key={i}>{i}</Span>
))}
阅读 1.4k
2 个回答

react 中并不包含 v-for 类似的指令,需要手动构建一个列表,你写的方式是比较常用的方式。

react没有类似vue的这种指令 不过你可以自己封装个组件做 类似这样

const Reapeat=({length,Com,...comProps})=>{
        return Array.from({length},(item,index)=>(<Com index={index} key={index} {...comProps} 
        />));
};
    const Span=props=>(<span style={{color:props.color}}>{props.index}</span>);
    const App=props=>(<Reapeat length={10} Com={Span} color={'red'} />);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题