简体   繁体   English

如何将道具传递给延迟加载组件?

[英]how to pass props to lazy loading components?

I'm trying to make website authentication. 我正在尝试进行网站认证。 I tried to pass props to lazy loading modules but suddenly i encountered error "Uncaught (in promise) TypeError: w is not a function" 我试图将道具传递给延迟加载模块,但突然遇到错误“未捕获(承诺)TypeError:w不是函数”

I'm using react with apollo client, react router and graphql query is perfectly working and lazy component's rendering too but i can't pass props to lazy loading component 我正在使用与阿波罗客户端进行反应,反应路由器和graphql查询也可以正常工作,并且懒惰组件的渲染也可以,但是我无法将道具传递给懒惰加载组件

// App.js
const SignIn = lazy(() => import('pages/signin'));

function App(props) {
  // state to represent user signed in to the site
  let [auth, setAuth] = useState(false);

  function handleAuth (boolean) {
    setAuth(boolean);
  }

  return (
    <>
      <main>
        <Suspense fallback={<div>Loading...</div>}>
          <Switch>
            <Route exact path="/" component={HomePage} />
            <Route 
              path="/signin"
              auth={auth}
              handleAuth={handleAuth}
              render={props => (<SignIn {...props} /> )}
            />
// signin.js
let auth = props.auth;
let handleAuth = props.handleAuth;

return (
    <ApolloConsumer>
      {client => {
        return (
          <>
            <div className="signin-div">
              <h1>Sign in</h1>

              <form
                onSubmit={async e => {
                  e.preventDefault();
                  const { data } = await client.query({
                    query: SIGNIN_ACCOUNT,
                    variables: { email, password }
                  });
                  if (data.signin) {
                    handleAuth(true);
                  }
                }}
                className="signin-form"
              >

I expected that handleAuth(true) will perfectly work but I took error that handleAuth(true) is not a function. 我期望handleAuth(true)可以正常工作,但是我出错了,handleAuth(true)不是一个函数。 below is the error message "Uncaught (in promise) TypeError: w is not a function" 下面是错误消息“未捕获(在承诺中)TypeError:w不是一个函数”

How should I pass props to lazy loading modules? 如何将道具传递给延迟加载模块?

No rocket science here just pass it props directly to signin component, the reason you are sending the props separately is because those props are those recieved from then route. 这里没有火箭科学会直接将道具传递给登录组件,您单独发送道具的原因是因为这些道具是从那时开始接收的那些道具。

 return (
        <>
          <main>
            <Suspense fallback={<div>Loading...</div>}>
              <Switch>
                <Route exact path="/" component={HomePage} />
                <Route 
                  path="/signin"
                  auth={auth}

                  render={props => (<SignIn handleAuth={handleAuth} {...props} /> )}
                />

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

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