Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.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
Javascript 反应附加到数组_Javascript_Arrays_Reactjs_Reactstrap - Fatal编程技术网

Javascript 反应附加到数组

Javascript 反应附加到数组,javascript,arrays,reactjs,reactstrap,Javascript,Arrays,Reactjs,Reactstrap,我正在使用一个面板主题 侧菜单由菜单对象数组填充 我希望: 从XHR填充数组 将原始数据blob设置为全局变量,以便应用程序的其余部分使用 这就是我正在尝试的 class ClusterItems extends Component { constructor (props) { super(props) this.state = { apiurl: ' ', clusterItems: [ { name: 'Clu

我正在使用一个面板主题

侧菜单由菜单对象数组填充

我希望:

  • 从XHR填充数组

  • 将原始数据blob设置为全局变量,以便应用程序的其余部分使用

  • 这就是我正在尝试的

    class ClusterItems extends Component {
      constructor (props) {
        super(props)
        this.state = {
          apiurl: ' ',
          clusterItems: [
            {
              name: 'Clusters',
              title: true,
              wrapper: { element: '', attributes: {} },
              class: ''
            },
            {
              divider: true
            }
          ]
        }
      }
      componentDidMount () {
        const apiurl = 'http://validapiurl/clusters'
        axios.get(apiurl).then(results => (
          this.state.clusterItems.push({
            icon: 'icon-layers',
            name: item.clusterName,
            url: '/clusters' + item.clusterName + '/instances'
          })
        ))
      }
    }
    

    我缺少的部分是如何
    将this.state.clustriems导出为数组,以及如何全局导出数据blob
    集群

    如果您想将结果添加到state以便使用,您可以执行类似
    results=>this.setState({clustriems:this.state.clustriems.concat)的操作(IteemYouwanttoAdd)})
    为什么您需要全局?使用react,您可以将数据作为道具传递给子组件。react通常就是这样做的。但是,如果您想要应用程序状态,您可以使用诸如redux或MobX之类的状态库。请描述您为什么需要全局,或许我们可以为您指出正确的方向:)