编译失败:ReactJS中未定义'props'no-undef

萨玛斯

我试图将一个函数传递给一个React Router,但尽管进行了一些调整,但它给了我一个错误。我尝试将函数放在render()中,在params props之前添加了函数,但似乎没有任何效果。如何在Route和Redirect标签之间传递函数以选择性返回?

import React, { Component } from 'react';
import { Switch, Route, Redirect } from 'react-router-dom';
import DogList from './DogList';
import DogDetails from './DogDetails';

class Routes extends Component {
  constructor(props) {
    super(props);
    this.getDog = this.getDog.bind(this);
  }

  getDog() {
      let name = props.match.params.name;
      let currDog = this.props.dogs.find(
          dog => dog.name.toLowerCase() === name.toLowerCase()
      );
      if(currDog != undefined) {
        return <DogDetails {...props} dog={currDog} /> ;
      } else {
        return <Redirect to="/dogs" />
      }
    }

  render() {
    
    return(
      <Switch>
        <Route exact path='/dogs' render= {() => <DogList dogs={this.props.dogs} />} />
        <Route exact path='/dogs/:name' render={(props) => {this.getDog()}} />
        <Redirect to='/dogs' />
      </Switch>
    );
  }
}

export default Routes;
舒哈丁

我建议您分隔组件,因为可能会有很多路由,因此,您可能无法在一个组件中管理它们。

无论如何,请尝试将props作为参数发送给函数。

您应该使用BrowserRouter包装交换机。

import React, { Component } from 'react';
import { Switch, Route, Redirect, BrowserRouter as Router } from 'react-router-dom';
import DogList from './DogList';
import DogDetails from './DogDetails';

class Routes extends Component {
  constructor(props) {
    super(props);
    this.getDog = this.getDog.bind(this);
  }

  getDog(props) {
      const { dogs } = this.props;

      let name = props.match.params.name;
      let currDog = dogs.find(
          dog => dog.name.toLowerCase() === name.toLowerCase()
      );
      if(currDog != undefined) {
        return <DogDetails {...props} dog={currDog} /> ;
      } else {
        return <Redirect to="/dogs" />
      }
    }

  render() {
    const { dogs } = this.props;
    
    return(
      <Router>
      <Switch>
        <Route exact path='/dogs' render= {() => <DogList dogs={dogs} />} />
        <Route exact path='/dogs/:name' render={(props) => this.getDog(props)} />
        <Redirect to='/dogs' />
      </Switch>
      </Router>
    );
  }
}

请记住,此反应路由器文档。这是一个很好的例子。https://reactrouter.com/web/guides/quick-start

这里有一个完整的例子。https://codesandbox.io/s/sleepy-ishizaka-n0433?file=/src/App.js

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

'$' 未定义 no-undef reactjs ajaxcall

来自分类Dev

错误未能编译未定义 no-undef

来自分类Dev

未定义'useState'no-undef React

来自分类Dev

未定义searchChange no-undef

来自分类Dev

错误:“$”未定义。[no-undef]

来自分类Dev

'state' 未定义(no-undef)

来自分类Dev

React 函数 - 未定义 no-undef

来自分类Dev

React:组件文件中未定义'p'no-undef

来自分类Dev

未定义handleChange在react中没有no-undef

来自分类Dev

ESLint:未定义“头盔” react / jsx-no-undef

来自分类Dev

行105:14:未定义'accountSelector'no-undef

来自分类Dev

React和Redux:获取未定义no-undef错误

来自分类Dev

未定义'组件'react / jsx-no-undef

来自分类Dev

反应 TinyMce:'tinymce' 未定义 no-undef

来自分类Dev

ReactJS-{this.props.children}未定义

来自分类Dev

TypeError:_this.props.match是未定义的ReactJS

来自分类Dev

reactjs错误:TypeError:无法读取未定义的属性“ props”

来自分类Dev

Reactjs教程:无法读取未定义的属性“ props”

来自分类Dev

ReactJS组件函数调用此函数,props未定义

来自分类Dev

Reactjs/Redux -- this.props.router 未定义

来自分类Dev

未捕获的TypeError:无法读取reactJS中未定义的属性'props'

来自分类Dev

无法读取功能组件中未定义的属性“ props”

来自分类Dev

从getStaticProps返回的组件中获取未定义的props

来自分类Dev

this.props 在类方法中未定义

来自分类Dev

jQuery.event.props未定义

来自分类Dev

this.props.navigation未定义(createStackNavigator)

来自分类Dev

TypeError:this.props.obj未定义

来自分类Dev

反应this.props.children是未定义的

来自分类Dev

TypeError:this.props。未定义