【问题标题】:Reactjs component - looping through json object - not rendering markupReactjs 组件 - 循环通过 json 对象 - 不呈现标记
【发布时间】:2017-09-15 22:53:17
【问题描述】:

我正在尝试遍历这个 json 以呈现一组段落——尽管当我获得对内部循环的访问权限时——标记没有呈现——它只是空白?

我可以使用控制台日志读取对象详细信息 - 但渲染标记似乎没有影响 - 它是否失去了范围?

这里的目标是渲染一批由这个 json 驱动的句子,所以回想起来。

Score for emotional distress and hyperactivity and concentration is close to the average 
Score for behavioural difficulties is slightly raised
Score for peer difficulties and overall stress is very high 

Score for kind and helpful behaviour is very low

json

"contents": {
                "1": {
                  "1": {
                    "alert": "green",
                    "values": ["emotional distress", "hyperactivity and concentration"],
                    "label": "close to the average"
                  },
                  "2": {
                    "alert": "yellow",
                    "values": ["behavioural difficulties"],
                    "label": "slightly raised"
                  },
                  "4": {
                    "alert": "red",
                    "values": ["peer difficulties", "overall stress"],
                    "label": "very high"
                  }
                },
                "2": {
                  "4": {
                    "alert": "red",
                    "values": ["kind and helpful behaviour"],
                    "label": "very low"
                  }
                }
              }

组件。

var YourCurrentStanding = React.createClass({

    createSentence : function(array) {
      if (array.length === 1) {
        return array[0];
      } else if (array.length === 0) {
        return "";
      }
      return array.slice(0, array.length - 1).join(", ") + " and " + array[array.length - 1];
    },

    render: function() {
      var that = this;

      return (
        <div className="component-container">
          {
            Object.keys(this.props.data.contents).map((key, i) => {      

              console.log("key", key);
              console.log("index", i);

              var ordered = this.props.data.contents[key];
              var that = this;

              return (
                <div className="bands" key={i}>
                  {
                    Object.keys(ordered).map(function(key) {
                        console.log(key);          // the name of the current key.
                        console.log(ordered[key]);   // the value of the current key.
                           return (
                            <p className="large-text margin-bottom">Score for {that.createSentence(ordered[key].values)} is <b className={ordered[key].alert+"-text"}>{ordered[key].label}</b></p>
                          ); 
                    })
                  }

                </div>
              )

            })
          }
        </div>
      );
    }
});

当前演示

var data = {
  contents: {
    "1": {
              "1": {
                "alert": "green",
                "values": ["emotional distress", "hyperactivity and concentration"],
                "label": "close to the average"
              },
              "2": {
                "alert": "yellow",
                "values": ["behavioural difficulties"],
                "label": "slightly raised"
              },
              "4": {
                "alert": "red",
                "values": ["peer difficulties", "overall stress"],
                "label": "very high"
              }
            },
            "2": {
              "4": {
                "alert": "red",
                "values": ["kind and helpful behaviour"],
                "label": "very low"
              }
            }
  }
};



var YourCurrentStanding = React.createClass({

createSentence : function(array) {
  if (array.length === 1) {
    return array[0];
  } else if (array.length === 0) {
    return "";
  }
  return array.slice(0, array.length - 1).join(", ") + " and " + array[array.length - 1];
},
   
render: function() {
  var that = this;

  return (
    <div className="component-container">
      {
        Object.keys(data.contents).map((key, i) => {      
      
          console.log("key", key);
          console.log("index", i);
          
          var ordered = data.contents[key];
          var that = this;

          return (
            <div className="bands" key={i}>
              {
                Object.keys(ordered).map(function(k, j) {
                    console.log(k);          // the name of the current key.
                    console.log(ordered[k]);   // the value of the current key.
                       return (
                        <p key={j} className="large-text margin-bottom">Score for {that.createSentence(ordered[k].values)} is <b className={ordered[k].alert+"-text"}>{ordered[k].label}</b></p>
                      ); 
                })
              }

            </div>
          )
          
        })
      }
    </div>
  );
}
});


ReactDOM.render(<YourCurrentStanding />, document.getElementById("app"));
<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"/>

--

问题 1 -

在我的构建中——它呈现不同的段落而不是连接句子。

【问题讨论】:

  • 重构你的代码,这样你就可以记录你期望在那里的数据。这是一场噩梦,因为它是为调试而编写的。
  • -- 但我正在获取控制台日志 - 在此处显示数据 - console.log(key); // 当前键的名称。 console.log(ordered[key]); // 当前键的值。
  • ^ 我可以看到它创建了两个波段 div...然后它访问内容数组中的正确项目 - 这确实需要排序,所以顶部的最高数字。
  • 将你的 React 组件构造捕获到一个变量中并记录下来。
  • 我有? -- 它停止渲染段落标签 -- 即使使用虚拟数据 -- 我可以看到控制台日志键和有序[key]

标签: javascript json reactjs


【解决方案1】:

试试Object.keys(ordered).map 而不是Object.keys(ordered).forEach

【讨论】:

  • 这对@Oblosys 产生了影响——尽管它只渲染了 1 行?
  • 应该产生一个div,里面有一堆ps。如果这最终出现在一行上,那可能是因为样式。您可以在浏览器中使用检查来检查生成的结构。 (并且您可能希望删除 return 语句周围的多余花括号)
  • 它似乎在这个演示中产生了一串单词 - 在我的构建中它没有构建一串连接词 - 但它产生了多个段落。 @Oblosys
  • -- 哟 @Oblosys 你修好了 -- 为什么它会在 forEach 而不是地图上中断?
  • 你是对的——演示 css 覆盖了标记——我的数据与现在的演示不同——我的数据在我的构建中正确呈现——我认为你解决了这个问题——我我只是累了。
猜你喜欢
  • 1970-01-01
  • 2018-07-15
  • 1970-01-01
  • 2014-01-13
  • 2021-05-29
  • 2016-10-11
  • 1970-01-01
  • 2018-04-26
  • 1970-01-01
相关资源
最近更新 更多