【问题标题】:Warning: Failed prop type: The prop `to` is marked as required in `Link`, but its value is `undefined`警告:失败的道具类型:道具“to”在“链接”中标记为必需,但其值为“未定义”
【发布时间】:2019-06-25 21:22:10
【问题描述】:
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import { Jumbotron, Grid, Row, Col, Image, Button } from 'react-bootstrap';
import './Home.css';

export default class Home extends Component{
render() {
    return (
        <Grid>
            <Jumbotron>
                <h2>Welcome to Judic-O Couture.io</h2>
                <p>Understang the basics of react, react-bootstrap & react-router-dom.</p>
            </Jumbotron>
            <Link>
                <Button bsStyle="primary">About</Button>
            </Link>
        </Grid>
    );
  };
};

看不到我的错误,但我知道其中存在错误。我已经转到链接标签,因为那是 react-router-dom 中错误指向的地方

【问题讨论】:

  • 错误信息非常明确,链接组件需要一个道具 to 而你没有提供它。
  • 是的,谢谢 nubinub,我想我可以继续了
  • 谢谢你们,特别是 ma_dev_15

标签: reactjs react-router


【解决方案1】:

我在使用&lt;Link to={this.props.link}&gt;时遇到了这个问题

因为 props.link 在用户输入之前被设置为未定义。
快速破解将其更改为&lt;Link to={this.props.link || '/'}&gt; 您可以简单地将/ 替换为/# 或任何其他默认路由(路径)。

【讨论】:

    【解决方案2】:

    在Link中你必须添加“to”,如果你没有真正使用路由器做任何你不应该使用Link的事情,但如果出于某种原因你需要它,那么就这样做

    &lt;Link to="/#"&gt;&lt;Button bsStyle="primary"&gt;About&lt;/Button&gt;&lt;/Link&gt;

    再一次,如果你真的在路由器上使用这个关于你应该使用

    【讨论】:

      【解决方案3】:

      阅读 react-router 的文档。

      https://reacttraining.com/react-router/web/api/Link

      to 组件需要 Link 组件。

      【讨论】:

        【解决方案4】:
                <Link>
                    <Button bsStyle="primary">About</Button>
                </Link>
        

        这里需要在Link中添加to

                <Link to="/about">
                    <Button bsStyle="primary">About</Button>
                </Link>
        

        【讨论】:

          猜你喜欢
          • 2018-11-23
          • 1970-01-01
          • 2021-06-06
          • 2018-09-22
          • 2020-12-24
          • 1970-01-01
          • 2022-07-20
          • 2018-03-23
          • 2017-12-11
          相关资源
          最近更新 更多