javascript - (React Native) 显示数组中项目的卡片列表

标签 javascript reactjs mobile native

我有两个数组,比方说 单词和定义

export default class Dictionary extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
        word: [],
        definition:[],
        index: 0
    };    
}

我有一个 Prop

<Card word = {w} definition = {d}/> 

我想为数组中的每个单词/定义对显示这些卡片的列表。如果有 5 个单词/定义,那么我希望其中 5 个卡片显示在 ScrollableView 中。我怎样才能做到这一点?谢谢!

最佳答案

您可以使用 Array.prototype.map Array.prototype.map 函数回调中的第二个参数是索引。您可以使用该索引来显示相应的definition项,如下所示

export default class Dictionary extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
        word: ["a","b","c"],
        definition:["a","b","c"],
        index: 0
    };    

    render() {
       <div>
       {this.state.word.map((w,i) => {
          return <Card word = {w} definition = {this.state.definition[i]}/> 
       })}
       </div>
    }
}

关于javascript - (React Native) 显示数组中项目的卡片列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48031260/

相关文章:

javascript - 检测页面是否从应用程序打开

javascript - 使用javascript关闭当前网页onclick按钮?

.htaccess - 重定向旧静态移动网站的最佳实践

javascript - 为 Firefox 和 IE 定制滚动条设计

reactjs - React/TypeScript/Redux/Thunk 操作未调度且状态未更新

javascript - MobX 'this' 在 setter 操作中未定义

javascript - 使用包含凭据的 URL 进行请求

IOS Swift 在屏幕尺寸改变时调整标签字体大小

javascript - IE 中的 HTML 摘要标签

javascript - 如何在一个二维 Canvas 上下文上一次绘制多条线?多用户画板