【问题标题】:Expected an assignment or function call and instead saw an expression no-unused-expressions?期望一个赋值或函数调用,而是看到一个表达式 no-unused-expressions?
【发布时间】:2021-11-17 20:28:54
【问题描述】:
import { Route, Redirect } from 'react-router-dom';
import React, { useContext } from 'react';

import AuthContext from '../contexts/AuthContext';

function PrivateRoute({ children, ...rest }) {
    const auth = useContext(AuthContext);

    return(
        <Route
        {...rest}
        render={({ location }) => {
            auth.token ? (
                children
            ) : (
                <Redirect
                to={{
                    pathname: '/login',
                    state: { from: location },
            }}
          />
        )
      }} 
    />
  )
}

export default PrivateRoute;

期待一个赋值或函数调用,却看到一个表达式 no-unused-expressions。我做错了什么

13:13 出错

  auth.token ? (

【问题讨论】:

  • 你的render 函数没有返回任何东西。未分配或返回条件/三元表达式的结果。

标签: javascript reactjs


【解决方案1】:

你需要返回三元运算符。

return auth.token ? (
            children
        ) : (
            <Redirect
            to={{
                pathname: '/login',
                state: { from: location },
        }}

【讨论】:

    猜你喜欢
    • 2021-08-23
    • 2019-08-15
    • 2020-12-10
    • 2020-04-21
    • 2019-11-23
    • 2020-11-20
    • 1970-01-01
    • 2020-07-31
    • 2021-05-20
    相关资源
    最近更新 更多