javascript - 在循环中响应 JS/JSX if 语句

标签 javascript reactjs

我在理解 React 逻辑时遇到问题。为什么这个 IF 不起作用?您可以假设所有类都在那里,并且循环正在运行。即使条件似乎有效,但我页面上的输出仍然是空白。

    var Feature = React.createClass({
    componentDidMount: function(){
        $('input.rating[type=number]').rating();
        initFeatureInstallReady(this.props.feature.feature_id);
    },
    render: function(){

        var backgroundColor = "white";
        var item;
        var props = this.props;

        this.props.feature.slides.map(function(slide, i){

            if(props.feature.feature_id == "start-home-away" && slide.slide_id == 'review'){

                item = <div className={i == 0 ? "item active" : "item"} key={i} id={slide.slide_id}>
                            <FeatureReviewHomeAway feature={props.feature} slide={slide} />
                        </div>;

            }else if(props.feature.feature_id == "start-goto-sleep" && slide.slide_id == 'review'){

                item = <div className={i == 0 ? "item active" : "item"} key={i} id={slide.slide_id}>
                            <FeatureReviewHomeAway feature={props.feature} slide={slide} />
                        </div>;

            }else{

                item = <div className={i == 0 ? "item active" : "item"} key={i} id={slide.slide_id}>
                            <FeatureMain_Step feature={props.feature} slide={slide} />
                        </div>;
            }

        });

        return (
            <div className="carousel slide carousel-feature" data-ride="" data-interval="false" id={this.props.feature.feature_id}>
                <div className="carousel-inner" role="listbox" style={{backgroundColor: backgroundColor}}>
                    {item}
                </div>
            </div>
        );
    }
});

我错过了什么?

最佳答案

您没有正确使用 .map。它将转换数组中的每个元素,并返回一个新数组。您想要保存该数组并显示它,而不是将变量保存在 inside 转换函数中。

var Feature = React.createClass({
    componentDidMount: function(){
        $('input.rating[type=number]').rating();
        initFeatureInstallReady(this.props.feature.feature_id);
    },
    render: function(){

        var backgroundColor = "white";
        var props = this.props;

        // convert each slide into a <div> in a brand new array
        //// `.map` will create a new array full of divs
        var items = this.props.feature.slides.map(function(slide, i){

            if(props.feature.feature_id == "start-home-away" && slide.slide_id == 'review'){
                // return this slide converted to a <div>
                return <div className={i == 0 ? "item active" : "item"} key={i} id={slide.slide_id}>
                            <FeatureReviewHomeAway feature={props.feature} slide={slide} />
                        </div>;

            }else if(props.feature.feature_id == "start-goto-sleep" && slide.slide_id == 'review'){
                // return this slide converted to a <div>
                return <div className={i == 0 ? "item active" : "item"} key={i} id={slide.slide_id}>
                            <FeatureReviewHomeAway feature={props.feature} slide={slide} />
                        </div>;

            }else{
                // return this slide converted to a <div>
                return <div className={i == 0 ? "item active" : "item"} key={i} id={slide.slide_id}>
                            <FeatureMain_Step feature={props.feature} slide={slide} />
                        </div>;
            }

        });

        return (
            <div className="carousel slide carousel-feature" data-ride="" data-interval="false" id={this.props.feature.feature_id}>
                <div className="carousel-inner" role="listbox" style={{backgroundColor: backgroundColor}}>
                    {items}
                </div>
            </div>
        );
    }
});

关于javascript - 在循环中响应 JS/JSX if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32359765/

相关文章:

javascript - 在每次页面加载时滚动条到 iFrame 容器 Div 的顶部?

javascript - 在外部函数中设置状态/使用状态 react

reactjs - 功能组件在 setState 更新 Hook 后不重新渲染

javascript - 联合泛型类型的行为应该是什么,例如 Array<A | B>?

javascript - 位置粘性不重叠

javascript - 如何根据文本框是否为空来禁用或启用按钮?

javascript - Jquery Ajax Post 中的 CORS 问题

reactjs - 使用无服务器函数通过 Mongoose 在 MongoDB 数据库中 findById,并以 React 形式呈现结果 - 不起作用

reactjs - React Apollo GraphQL : Expecting a parsed GraphQL document. 也许您需要将查询字符串包装在 "gql"标签中?

javascript - VSCode 不会对模板字符串内的内容进行样式设置