在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ 使用React.Fragment報(bào)錯(cuò)

使用React.Fragment報(bào)錯(cuò)

在學(xué)習(xí)react,redux等知識(shí),寫了一個(gè)簡(jiǎn)單的404頁面,如下

NonExistent.js

import React from 'react';

function NonExistent(props) {

    return (
        <div>
            error
        </div>
    )
}

export default NonExistent

調(diào)用的時(shí)候用了Route

App.js

import { Route, Link, withRouter, Switch, Redirect } from 'react-router-dom';
...
<Switch>
...
    <Route exact path={/error} component={NonExistent} />
...
</Switch>

這樣是可以在切到/error路徑時(shí)正常顯示頁面的。

然后我聽說了有React.Fragment的用法,于是把頁面改成
NonExistent.js

import React from 'react';

function NonExistent(props) {

    return (
        <React.Fragment>
            error
        </React.Fragment>
    )
}

export default NonExistent

切到/error路徑時(shí),就報(bào)錯(cuò)了

React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in.

圖片描述

莫非是和Route結(jié)合了不能使用這個(gè)語法?
或者是我寫錯(cuò)了什么?
我用google和火狐瀏覽器都是這個(gè)錯(cuò)誤。求指點(diǎn)。

回答
編輯回答
入她眼

React.Fragment一般是用來處理多個(gè)元素返回的情況,F(xiàn)ragment本身不轉(zhuǎn)換為任何dom元素,是個(gè)空標(biāo)簽,比如

render() {
  return (
    <Fragment>
      <li>hi</li>
      <li>原罪</li>
    </Fragment>
  );
}

如果不用Fragment,就得用數(shù)組包裹,

render() {
  return (
    [
      <li key="a">hi</li>,
      <li key="b">原罪</li>,
    ]
  );
}

你如果值返回一個(gè)字符串,那就直接返回就好了

render() {
  return 'error';
}
2018年5月1日 22:43