Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.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 React,这在render()和componentDidMount()中未定义_Javascript_Reactjs_Ecmascript 6 - Fatal编程技术网

Javascript React,这在render()和componentDidMount()中未定义

Javascript React,这在render()和componentDidMount()中未定义,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,我正在用我所有的路由遍历一个字符串数组。然后检查当前window.location.href`是否包含任何这些路由,如果包含,则获取匹配的ref并添加一个类来更改css 我在返回语句之前的componentDidMount()和render()中尝试了这个方法。React一直告诉我,此未定义 react/src/pages/nav.js 安慰 并在控制台中选中此,通过将其分配给另一个变量,然后在渲染中使用新创建的变量访问道具,它返回未定义的,通过将其分配给另一个变量,然后在渲染中使用新创建的变量

我正在用我所有的路由遍历一个字符串数组。然后检查当前window.location.href`是否包含任何这些路由,如果包含,则获取匹配的ref并添加一个类来更改css

我在返回语句之前的
componentDidMount()
render()
中尝试了这个方法。React一直告诉我,
未定义

react/src/pages/nav.js 安慰
并在控制台中选中此,通过将其分配给另一个变量,然后在渲染中使用新创建的变量访问道具,它返回
未定义的

,通过将其分配给另一个变量,然后在渲染中使用新创建的变量访问道具,您应该尝试
让此=此请在堆栈跟踪中包含完整错误。都德错误表示完全不同的内容错误与
无关。这意味着
this.refs[route]
(对于
route
的某些值)或
this.refs.root
不存在。添加调试器语句或暂停异常,检查变量并找出何时得到
未定义的
此。不应在
渲染中使用refs
,部分原因是refs仅在第一次渲染后存在。您的组件可能需要一些重构。在上查看React的文档。你应该尝试
let\u this=this请在堆栈跟踪中包含完整错误。都德错误表示完全不同的内容错误与
无关。这意味着
this.refs[route]
(对于
route
的某些值)或
this.refs.root
不存在。添加调试器语句或暂停异常,检查变量并找出何时得到
未定义的
此。不应在
渲染中使用refs
,部分原因是refs仅在第一次渲染后存在。您的组件可能需要一些重构。在上查看React的文档。
      render() {
        let allRoutes = [];
        this.props.route.childRoutes.forEach(route => {
          allRoutes.push(route.path)
        })
        const url = window.location.href;
        allRoutes.forEach(route => {
          if (url.includes(route)) {
            this.refs[route].classList.add('active')
          } else {
            this.refs.root.classList.add('active')
          }
        })
Uncaught TypeError: Cannot read property 'classList' of undefined
    at eval (eval at <anonymous> (http://localhost:3000/assets/bundle.self-952529963c9d96b227c46b5e763e815b470d4de62fa7b9b876d16aa3475efce8.js?body=1:3270:2), <anonymous>:50:29)
    at Array.forEach (native)
    at Nav.render (eval at <anonymous> (http://localhost:3000/assets/bundle.self-952529963c9d96b227c46b5e763e815b470d4de62fa7b9b876d16aa3475efce8.js?body=1:3270:2), <anonymous>:48:17)
    at eval (eval at <anonymous> (http://localhost:3000/assets/bundle.self-952529963c9d96b227c46b5e763e815b470d4de62fa7b9b876d16aa3475efce8.js?body=1:2538:2), <anonymous>:796:21)
    at measureLifeCyclePerf (eval at <anonymous> (http://localhost:3000/assets/bundle.self-952529963c9d96b227c46b5e763e815b470d4de62fa7b9b876d16aa3475efce8.js?body=1:2538:2), <anonymous>:75:12)
    at ReactCompositeComponentWrapper._renderValidatedComponentWithoutOwnerOrContext (eval at <anonymous> (http://localhost:3000/assets/bundle.self-952529963c9d96b227c46b5e763e815b470d4de62fa7b9b876d16aa3475efce8.js?body=1:2538:2), <anonymous>:795:25)
    at ReactCompositeComponentWrapper._renderValidatedComponent (eval at <anonymous> (http://localhost:3000/assets/bundle.self-952529963c9d96b227c46b5e763e815b470d4de62fa7b9b876d16aa3475efce8.js?body=1:2538:2), <anonymous>:822:32)
    at ReactCompositeComponentWrapper.performInitialMount (eval at <anonymous> (http://localhost:3000/assets/bundle.self-952529963c9d96b227c46b5e763e815b470d4de62fa7b9b876d16aa3475efce8.js?body=1:2538:2), <anonymous>:362:30)
    at ReactCompositeComponentWrapper.mountComponent (eval at <anonymous> (http://localhost:3000/assets/bundle.self-952529963c9d96b227c46b5e763e815b470d4de62fa7b9b876d16aa3475efce8.js?body=1:2538:2), <anonymous>:258:21)
    at Object.mountComponent (eval at <anonymous> (http://localhost:3000/assets/bundle.self-952529963c9d96b227c46b5e763e815b470d4de62fa7b9b876d16aa3475efce8.js?body=1:2178:2), <anonymous>:46:35)
allRoutes.forEach(route => {
  if (url.includes(route)) {
    debugger