简体   繁体   English

将道具传递到React Router的链接组件

[英]Pass props to React Router's Link Component

I'm passing location props to React Router's Link. 我正在将位置道具传递给React Router的链接。 It successfully navigates me away, but when I click "back" to go back to the previous page, I get an error: 它成功使我离开,但是当我单击“后退”返回上一页时,出现错误:

TypeError Cannot Read property "statusUser" Undefined TypeError无法读取属性“ statusUser”未定义

It looks like the props are getting messed up when I navigate back. 当我向后导航时,似乎道具弄乱了。

Sidebar.js Sidebar.js

Class Sidebar extends Commponent{
render(){    
    return(
     <Link to={
                {
                  pathname: `/user`,
                  state:{
                    statusUser: 'abc',
                  }
                 }
               }><Button>User Menu</Button>
 }
          )
}

User.jsx User.jsx

class user extends Component {
  render(){    
    return(
        <UserTable status={this.props.location.state.statusUser}/>
    )
  }
}

UserTable.jsx UserTable.jsx

 class UserTable extends Component{
         render(){    
            return(
                     <Link to={
                {
                  pathname: `/user/detail/${this.props.status}`,
                  state:{
                    statusUser: this.props.status,
                  }
                 }
               }><Button>Detail</Button>
            )
          }
    }

UserDetail.jsx UserDetail.jsx

   class UserDetail extends Component{
             render(){    
                return(
                      <p>{this.props.location.state.statusUser}</p>
                         <Link to={
                    {
                      pathname: `/user`,
                     }
                   }><Button>Back</Button>
                )
              }
        }

I think the problem is in User.js. 我认为问题出在User.js中。 How do I make the props fixed like using setState or stuff like that inside User.js file? 如何固定道具,例如使用setState或User.js文件中的东西?

Just to be able read the props property, and after reading it go back to the previous page by clicking "back" button. 只是为了能够读取props属性,并在读取后通过单击“后退”按钮返回到上一页。

I'm sorry I just really really new to React so any help would be really appreciate. 对不起,我只是React的真正新手,所以我们将不胜感激。 Thank you. 谢谢。

I believe you're getting that error when you click the Back button in UserDetail.jsx . 我相信当您单击UserDetail.jsx的“后退”按钮时,您会收到该错误。 You aren't passing a state object to User.jsx . 您没有将state对象传递给User.jsx

The User.jsx component tries to access this.props.location.state.statusUser , but state doesn't exist, and so it says it can't read the property statusUser on undefined ( state is undefined ). User.jsx组件尝试访问this.props.location.state.statusUser ,但是state不存在,因此它说无法读取undefined statusUser属性( stateundefined )。

Here's what you're missing, with a few typos fixed: 这是您所缺少的,并修正了一些拼写错误:

// UserDetail.jsx
class UserDetail extends Component {
  render() {
    return (
      <div>
        <p>{this.props.location.state.statusUser}</p>
        <Link
          to={{
            pathname: `/user`
            // (You aren't passing `state`)
          }}
        >
          <Button>Back</Button>
        </Link>  // <-- You weren't closing Link
      </div>
    );
  }
}
// User.jsx
class User extends Component {  // <-- Capital 'U'
  render() {
    return (
      <UserTable
        status={this.props.location.state.statusUser}  // <-- You require `state`
      />
    );
  }
}

As for how to fix it, the easiest way would be to pass a state object with your "Back" button in UserDetail.jsx . 至于解决方法,最简单的方法是使用UserDetail.jsx “返回”按钮传递state对象。

You can also set defaultProps in React. 您还可以在React中设置defaultProps I'm not sure if that's applicable to you, but it lets you provide a fallback for such things: 我不确定这是否适用于您,但可以让您提供备用信息:

Edit: Include example of defaultProps and propTypes. 编辑:包括defaultProps和propTypes的示例。

// User.jsx
class User extends Component {
  render() {
    return (
      <UserTable
        status={this.props.location.state.statusUser}
      />
    );
  }
}

// Maybe something you put in a different file
const defaultLocation = {
  state: {
    statusUser: '',  // <-- Your default statusUser
  },
};

User.defaultProps = {
  location: defaultLocation,
};

// import PropTypes from 'prop-types';
User.propTypes = {
  location: PropTypes.shape({
    state: PropTypes.shape({
      statusUser: PropTypes.string.isRequired,
    })
  })
};

I added PropTypes in the above example. 我在上面的示例中添加了PropTypes It's a way to set checks for the data you're requiring in your components. 这是一种对组件中所需数据进行检查的方法。

Typically, we don't put props into defaultProps if it isRequired , though, because it will never come to that. 通常,如果isRequired ,我们不会将props放入defaultProps ,因为它永远不会实现。 If you're unsure what default value to give statusUser , I'd recommend starting with making it required in propTypes , and then you can refactor in the future if the need arises. 如果不确定要给statusUser提供的默认值,建议从propTypes使其成为必需propTypes ,然后在将来需要时可以进行重构。


There were some bugs with your other code samples, too. 您的其他代码示例中也存在一些错误。 I've formatted them below, and fixed the bugs. 我在下面格式化了它们,并修复了错误。 I'll point out what I fixed in comments: 我会指出我在评论中修正的内容:

Sidebar.js Sidebar.js

class Sidebar extends Component {  // <-- Lowercase 'c'; "Component"
  render() {
    return (
      <Link
        to={{
          pathname: `/user`,
          state: {
            statusUser: "abc"
          }
        }}
      >
        <Button>User Menu</Button>
      </Link>  // <-- You weren't closing Link
    );
  }
}

UserTable.jsx UserTable.jsx

class UserTable extends Component {
  render() {
    return (
      <Link
        to={{
          pathname: `/user/detail/${this.props.status}`,
          state: {
            statusUser: this.props.status
          }
        }}
      >
        <Button>Detail</Button>
      </Link>  // <-- You weren't closing Link
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM