【问题标题】:JSX/React - Appending ListView ItemsJSX/React - 附加 ListView 项目
【发布时间】:2016-03-03 14:58:58
【问题描述】:

我仍然对 react 及其神秘的方式感到困惑。在 jQuery + HTML 中,可能有一个元素 <ListView> </ListView>,该元素后来由生成 <Item>s 的 for 循环填充。同样在角度上,您只需 ng-repeat。

这样做的反应方式是什么?我以前有这个工作,当我有一个 <ListView items={listItems}></ListView> 与 listItems 在其他地方定义为 listItems = {heading:"qwerty", label:"test"} 等但现在当我检查执行代码时,我看到每个 <Item> 创建的都是一个“ReactElement”类型,所以使用这种行为不支持。

任何想法将不胜感激!谢谢。

更新代码:

render:function(){
    //Do the rendering

    var listViewItemsToRender = this.state.ListViewItems.map((item) => {
        return (
            <Item><div style={itemContainerParent}> <div style={itemPlatform}> <h1>props.platform</h1> </div><div style={routeInfo}><h1>props.routeInfo</h1></div><div style={timeInfo}><div style={row}><p>props.timeArriving</p></div><div style={row}><p>props.timeLeaving</p></div></div></div></Item>
        )
    });



    return (
        <Page>
            <BannerHeader theme="prime" key="header" flex={0} textAlign="center">Train Times</BannerHeader>
            <h2>{this.state.subheading}</h2>
            <Container>
                <Padding>
                    <BasicSegment>
                        <ButtonGroup fluid>
                            <Button onClick={this.showDepartures} variation={this.state.departuresSelected}>Departures</Button>
                            <Button onClick={this.showArrivals} variation={this.state.arrivalsSelected}>Arrivals</Button>
                        </ButtonGroup>
                        <Listview items={listViewItemsToRender}></Listview>

                    </BasicSegment>
                </Padding>
            </Container>
        </Page>
    );
}

【问题讨论】:

    标签: reactjs jsx


    【解决方案1】:

    记住在 reactjs 中是单向数据流。所以你处理数据,用户界面会根据你的修改做出反应...... 所以你的渲染方法将是循环的地方......

    在您的渲染处理程序方法中,您将有类似...

    render : function(){
      return itemsTobeDisplayed = items.map(function(item){
          return yourCustomItemMethodWhichRendersAsingleItem(itm);
        });
    
    }
    

    看看 reactjs todo 示例,它显示了一个包含单个项目的列表

    https://github.com/tastejs/todomvc/tree/master/examples/react

    ...这可能会有所帮助。

    【讨论】:

    • 谢谢!正是我所追求的。
    • 更新了我的代码以显示我认为我学到的东西 - 这仍然不起作用,items 仍然是一个 ReactElements 数组。
    • 更改行
    • {listViewItemsToRender} 修复它!
  • 你现在正在混合状态和道具。在您的 h1 中,您可以访问 props.routeInfo。这将是 items.routeInfo。你的收藏在哪里?因为您在 this.state.ListViewItems 上调用 map ..
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签