首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >输入'boolean‘不能指定键入'IntrinsicAttributes & boolean’

输入'boolean‘不能指定键入'IntrinsicAttributes & boolean’
EN

Stack Overflow用户
提问于 2022-05-27 14:10:12
回答 1查看 1.2K关注 0票数 2

我遇到了一个问题,我的反应应用,我试图创造私人路线。但是,当I试图传递用户的状态(验证或未验证//true或false)时,我得到以下错误:

代码语言:javascript
复制
Type '{ IsAuthenticated: boolean; }' is not assignable to type 'IntrinsicAttributes & boolean'.ts(2322)

这是我的密码:

代码语言:javascript
复制
export default function App ():ReactElement {
  const params = useParams()
  const [IsAuthenticated, setAuthenticated] = useState(Boolean)
  axios.get(`${process.env.API_URL || 'http:///localhost'}:${process.env.API_PORT || 8000}/api/session`)
    .then(res => {
      console.log(res.data.IsAuthenticated)
      if (res.data.IsAuthenticated) return setAuthenticated(false)
      else return setAuthenticated(false)
    })
  return (
      <Router>
          <Routes>
            <Route path='/' element={<Home/>}/>
            <Route path='/' element={<ProtectedRoute IsAuthenticated={IsAuthenticated}/>}>
            <Route path="dashboard" element={<Main/>}/>
            <Route path='user/:userId' element={<Userdetail userId={params}/>}/>
            </Route>
            <Route
            path="*"
            element={<Navigate to="/" replace />}
            />
          </Routes>
      </Router>
  )
}```
EN

回答 1

Stack Overflow用户

发布于 2022-08-25 16:39:43

我遇到了这个错误消息,这是因为我输入了错误的组件道具。以下是我所拥有的一个简化版本:

代码语言:javascript
复制
// ParentComponent.tsx

const [selected, setSelected] = useState<readonly string[]>([]);

return <ChildComponent selected={selected} />;
代码语言:javascript
复制
// ChildComponent.tsx (❌ incorrect)

                     // ▼ this is all props, so needs destructuring
const ChildComponent = (selected: boolean): ReactElement => (
  // my component
);

export default ChildComponent;

我的错误在于没有破坏从反应中传递出来的道具,这解决了它:

代码语言:javascript
复制
// ChildComponent.tsx (✅ correct)

                     // ▼ destructured props correctly
const ChildComponent = ({selected}: {selected: boolean}): ReactElement => (
  // my component
);

export default ChildComponent;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72406724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档