Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过多个类访问JS数据_Python_Reactjs - Fatal编程技术网

Python 通过多个类访问JS数据

Python 通过多个类访问JS数据,python,reactjs,Python,Reactjs,我有一个名为MyDictionary的reactjs类,它呈现python文件发送的数据。此类返回字典结构。我只想通过一个单独的类访问MyDictionary中的几个元素,通过一个单独的类访问MyDictionary中的一组不同的元素。我尝试了React.createElement(另一个类,{,如下所示,但这不起作用…我缺少什么 class MyDictionary extends React.Component { render() { return this.props.results

我有一个名为MyDictionary的reactjs类,它呈现python文件发送的数据。此类返回字典结构。我只想通过一个单独的类访问MyDictionary中的几个元素,通过一个单独的类访问MyDictionary中的一组不同的元素。我尝试了
React.createElement(另一个类,{
,如下所示,但这不起作用…我缺少什么

class MyDictionary extends React.Component {
render() {
  return this.props.results.map((result, index) =>
    React.createElement(
      "div",
      { className: "col-sm-12" },
      React.createElement(SomeOtherClass, {
        key: result.id,
        name: result.name,
        index: result.index,
        activity: this.props.index + 1,
        images: this.props.image_labels
      })
    )
  );
 }
}
return MyDictionary;

看起来您的
回调函数中的这个
运算符不是您所期望的

解决这一问题的最简单方法是在map调用之前创建一个var to
this

var _this = this;
然后在回调中使用
\u this
而不是
this

您还应该了解javascript闭包

Edit:render()应返回一个根元素。

class MyDictionary extends React.Component {
  render() {
    var _this = this;
    var children = this.props.results.map((result, index) =>
      React.createElement(
        "div",
        { className: "col-sm-12" },
        React.createElement(SomeOtherClass, {
          key: result.id,
          name: result.name,
          index: result.index,
          activity: _this.props.index + 1, // using _this
          images: _this.props.image_labels // using _this
        })
      )
    );
    return React.createElement("div", null, children);
  }
}
return MyDictionary;

因此,
React.createElement(SomeOtherClass,{
是链接到多个类的正确方法吗?我以前没有注意到这一点,您还需要将元素包装在一个元素中,如a。您基本上是将
SomeOtherClass
嵌套在
MyDictionary
元素中。如果它有意义,那么它应该是好的。(如果你从HTML元素的角度考虑,MyDictionary是一个包含多个元素的字典。好吧,这很有帮助,泰。我想仍然存在的问题是……我可以从MyDictionary中访问子元素的值,并通过另一个类或多个类呈现它们吗?我该怎么做?我很迷茫,我一直在试图找出t。)他离开了好几天…比如…为什么不能通过MyDictionary访问元素。这个。道具。孩子们?以下是我认为你需要的一个很好的答案: