reactjs React未处理的拒绝(TypeError):无法读取未定义的属性'state'

noj0wjuj  于 2022-12-03  发布在  React
关注(0)|答案(3)|浏览(142)

通过表单创建新游戏后,我可以重定向到新创建的游戏的显示页面,但如果我想进入游戏索引页面,它会重新加载新创建的游戏的显示页面。我需要刷新才能进入游戏的索引页面。
因此,我创建了clearSubmit()方法,再次将hasSubmitted值更改为false,将该方法传递到show page的路由中,并在show page的component(GameShow.js)中调用clearSubmit()方法作为道具。
现在,当我提交表单创建一个游戏,我得到了这个错误:

React Unhandled Rejection (TypeError): Cannot read property 'state' of 
undefined

src/components/GameShow.js:14

11 | constructor(props) {
12 |   super(props)
13 |   this.state = {
> 14 |     game: this.props.location.state.selectedGame
15 |   }
16 | }

应用程序js

class App extends Component {

  constructor(props) {
  super(props)
  this.state = {
   games: [] ,
   name: '',
   img_url: '',
   genre: '',
   platforms: '',
   video_url: '',
   description: '',
   hasSubmitted: false,
   newGame: {}
 }

   this.handleSubmit = this.handleSubmit.bind(this)
   this.handleChange = this.handleChange.bind(this)
   this.clearSubmit = this.clearSubmit.bind(this) 
}

   handleChange(e){
    this.setState({
      [e.target.name]: e.target.value
    })
  }

   handleSubmit = (e) => {
    e.preventDefault();
    const { name, img_url, genre, platforms, video_url, description} = 
           this.state;

   axios.post('http://localhost:4000/api/games', { name: name, img_url: 
    img_url, genre: genre, platforms: platforms, video_url: video_url, 
    description: description })
     .then((result) => {
       this.setState({
         hasSubmitted: true,
         newGame: result.data
       })
    });
  }

  clearSubmit() {
    this.setState({
      hasSubmitted: false
    })
  }

  render() {
   return (
    <Router>
     <div>
      <main>
        <Route exact path='/games' render={() => {
          let newGame = this.state.newGame
          return this.state.hasSubmitted
            ? <Redirect to=
                   {{pathname:`/games/${this.state.newGame.name}`, 
                     state: {selectedGame: newGame}}} 
              />
            : <GameForm handleChange={this.handleChange}   
              handleSubmit={this.handleSubmit} games={this.state.games} 
              />
          } }
        />

        <Route exact path='/games' render={() => {
            return (
            <GameIndex handleChange={this.handleChange}   
              handleSubmit={this.handleSubmit} games={this.state.games} 
            />
          )
          } }
        />
        <Route path="/games/:name" render={() => {
            return (
              <GameShow clearSubmit={this.clearSubmit} />
            )      
          } }
     />
      </main>
    </div>

游戏索引.js**

class GameIndex extends Component {

    render() {
       let videoGames = this.props.games.map((game, i) =>{
       let pathname = `/games/${game.name}`

       return (
        <div key={i}>
          <p><Link to=
         {{ pathname, state: {selectedGame: game}}}> {game.name}</Link></p>
        </div>
    )
  })

游戏秀.js

class GameShow extends Component {
  componentDidMount() {
   this.props.clearSubmit()
 }

 constructor(props) {
  super(props)
  this.state = {
   game: this.props.location.state.selectedGame
 }
}

我不知道我错过了什么。

  • 谢谢-谢谢
xmakbtuz

xmakbtuz1#

我认为您没有将任何名为location的props对象传递到GameShow.js中(当您在App.js中呈现该对象时)。

kqlmhetl

kqlmhetl2#

您正在尝试访问

this.props.location.state.selectedGame

但位置未作为道具传递给此组件,
所以你的状态是不确定的

azpvetkf

azpvetkf3#

return this.state.hasSubmitted
            ? <Redirect to=
                   {{pathname:`/games/${this.state.newGame.name}`, 
                      state: {selectedGame: newGame}
                      }} 
              />
            : <GameForm handleChange={this.handleChange}   
              handleSubmit={this.handleSubmit} games={this.state.games} 
              />

false的情况下,你没有传递state: {selectedGame: newGame},所以,你的propsundefined,你得到这个错误。
您可以在这里传递空白对象state: {selectedGame: {} },例如

<GameForm handleChange={this.handleChange}   
                  handleSubmit={this.handleSubmit} games={this.state.games} ,state: {selectedGame: {} }
                  />

并在构造函数中进行代码清理。

this.state = {
   game: this.props.state && this.props.state.selectedGame
 }

相关问题