React:对象不可作为React子元素。

huangapple go评论62阅读模式
英文:

React : Objects are not valid as a React child

问题

错误:
对象不可作为React子元素(找到具有键{data,status,statusText,headers,config,request}的对象)。 如果您打算渲染一组子元素,请改用数组。

我已经找了好几个小时,还没有找出问题所在。 请帮我。
当会员注册成功时,会发生以下错误。

我以为这可能是问题。 但我不知道这是否正确。

RegisterForm.tsx

...
const RegisterForm = () => {
  const navigate = useNavigate();
  const dispatch = useDispatch<AppDispatch>();
  const { form, auth, authError, user } = useSelector(({ auth, user }) => ({
    form: auth.register,
    auth: auth.auth,
    authError: auth.authError,
    user: user.user,
  }));
...
useEffect(() => {
    if (authError) {
      if (authError === 409) {
        setError("fail 409");
      } else {
        setError("fail");
      }
      return;
    }

    if (auth) {
      dispatch(check());
    }
  }, [auth, authError, dispatch]);
...
return (
    <AuthForm
      type="login"
      form={form}
      onChange={onChange}
      onSubmit={onSubmit}
      error={error}
    />
  );
...

AuthForm.tsx

const AuthForm: React.FC<AuthFormProps> = ({
  type,
  form,
  onChange,
  onSubmit,
  error,
}) => {
  const text = textMap[type];
  return (
    <SBox>
      <h3>{text}</h3>
      <form onSubmit={onSubmit}>
        <SInput
          autoComplete="username"
          name="username"
          placeholder="id"
          onChange={onChange}
          value={form.username}
        />
        <SInput
          autoComplete="new-password"
          name="password"
          placeholder="password"
          type="password"
          onChange={onChange}
          value={form.password}
        />
        {type === "register" && (
          <SInput
            autoComplete="new-password"
            name="passwordConfirm"
            placeholder="passwordtwo"
            type="password"
            onChange={onChange}
            value={form.passwordConfirm}
          />
        )}
        <SError>{error && <div>{error}</div>}</SError>
        <Button>{text}</Button>
      </form>
    </SBox>
  );
};

auth.ts

...
export const register = createAsyncThunk(
  "auth/register",
  async ({ username, password  }: any, { rejectWithValue }) => {
    try {
      const res = await authAPI.register({ username, password });
      const {data, status, statusText} = res;
      return {data, status, statusText};
      
    } catch (err: any) {
      return rejectWithValue(err.response.status);
    }
  }
);
...
extraReducers: (builder) => {
    builder.addCase(register.fulfilled, (state, action) => {
      state.auth = action.payload
      state.authError = null;
    });
    builder.addCase(register.rejected, (state, action) => {
      state.authError = action.payload;
    });
...
英文:

Error :
Objects are not valid as a React child (found: object with keys {data, status, statusText, headers, config, request}). If you meant to render a collection of children, use an array instead.

I haven't been able to find out what's the problem for hours. Please help me.
The following error occurs when membership registration is successful.

I thought this was the problem. But I don't know if this is right.

RegisterForm.tsx

...
const RegisterForm = () =&gt; {
  const navigate = useNavigate();
  const dispatch = useDispatch&lt;AppDispatch&gt;();
  const { form, auth, authError, user } = useSelector(({ auth, user }) =&gt; ({
    form: auth.register,
    auth: auth.auth,
    authError: auth.authError,
    user: user.user,
  }));
...
useEffect(() =&gt; {
    if (authError) {
      if (authError === 409) {
        setError(&quot;fail 409&quot;);
      } else {
        setError(&quot;fail&quot;);
      }
      return;
    }

    if (auth) {
      dispatch(check());
    }
  }, [auth, authError, dispatch]);
...
return (
    &lt;AuthForm
      type=&quot;login&quot;
      form={form}
      onChange={onChange}
      onSubmit={onSubmit}
      error={error}
    /&gt;
  );
...

AuthForm.tsx

const AuthForm: React.FC&lt;AuthFormProps&gt; = ({
  type,
  form,
  onChange,
  onSubmit,
  error,
}) =&gt; {
  const text = textMap[type];
  return (
    &lt;SBox&gt;
      &lt;h3&gt;{text}&lt;/h3&gt;
      &lt;form onSubmit={onSubmit}&gt;
        &lt;SInput
          autoComplete=&quot;username&quot;
          name=&quot;username&quot;
          placeholder=&quot;id&quot;
          onChange={onChange}
          value={form.username}
        /&gt;
        &lt;SInput
          autoComplete=&quot;new-password&quot;
          name=&quot;password&quot;
          placeholder=&quot;password&quot;
          type=&quot;password&quot;
          onChange={onChange}
          value={form.password}
        /&gt;
        {type === &quot;register&quot; &amp;&amp; (
          &lt;SInput
            autoComplete=&quot;new-password&quot;
            name=&quot;passwordConfirm&quot;
            placeholder=&quot;passwordtwo&quot;
            type=&quot;password&quot;
            onChange={onChange}
            value={form.passwordConfirm}
          /&gt;
        )}
        &lt;SError&gt;{error &amp;&amp; &lt;div&gt;{error}&lt;/div&gt;}&lt;/SError&gt;
        &lt;Button&gt;{text}&lt;/Button&gt;
      &lt;/form&gt;
    &lt;/SBox&gt;
  );
};

auth.ts

...
export const register = createAsyncThunk(
  &quot;auth/register&quot;,
  async ({ username, password  } : any, { rejectWithValue }) =&gt; {
    try {
      const res = await authAPI.register({ username, password });
      const {data, status, statusText} = res;
      return {data, status, statusText};
      
    } catch (err : any) {
      return rejectWithValue(err.response.status);
    }
  }
);
...
extraReducers: (builder) =&gt; {
    builder.addCase(register.fulfilled, (state, action) =&gt; {
      state.auth = action.payload
      state.authError = null;
    });
    builder.addCase(register.rejected, (state, action) =&gt; {
      state.authError = action.payload;
    });
...

答案1

得分: 1

错误似乎来自这一行 &lt;SError&gt;{error &amp;&amp; &lt;div&gt;{error}&lt;/div&gt;}&lt;/SError&gt;

error 通常是一个对象。尝试 &lt;SError&gt;{error &amp;&amp; &lt;div&gt;{JSON.stringify(error)}&lt;/div&gt;}&lt;/SError&gt;

英文:

Error seems to be coming from this line &lt;SError&gt;{error &amp;&amp; &lt;div&gt;{error}&lt;/div&gt;}&lt;/SError&gt;

error is generally an object. Try &lt;SError&gt;{error &amp;&amp; &lt;div&gt;{JSON.stringify(error)}&lt;/div&gt;}&lt;/SError&gt;

答案2

得分: 0

首先,我很抱歉我的问题不够准确。
问题出在导航栏(NavBar)上。
错误地使用了{user},应修改为{user.username}并解决了问题。
如果是一个大的调味汁,可能会很难找到。

const NavBar = () => {
  const user = useSelector((state: RootState) => state.userReducer.user);
  const dispatch = useDispatch<AppDispatch>();
  const onLogout = () => {
    dispatch(logout());
  };

  return (
    <SNavBar>
      <Link to="/"> 
        <img src={logo} alt="logo" style={{ height: 30, marginTop: 4 }} />
      </Link>

      <Button to="/post">board</Button>
      <Button to="/mypage">mypage</Button>

      {user ? (
        <>
          <Button>{user.username}</Button>
          <Button onClick={onLogout}>logout</Button>
        </>
      ) : (
        <>
          <Button to="/login">login</Button>
          <Button to="/register">register</Button>
        </>
      )}
    </SNavBar>
  );
};

请注意,上面是你提供的代码的翻译部分。

英文:

First of all, I'm sorry that my question is not accurate.
The problem was with NavBar.
Mistake using {user} as it is
Modified to {user.username} and resolved..
If it was a large sauce, it would have been hard to find.

const NavBar = () =&gt; {
  const user = useSelector((state: RootState) =&gt; state.userReducer.user);
  const dispatch = useDispatch&lt;AppDispatch&gt;();
  const onLogout = () =&gt; {
    dispatch(logout());
  };

  return (
    &lt;SNavBar&gt;
      &lt;Link to=&quot;/&quot;&gt;
        &lt;img src={logo} alt=&quot;logo&quot; style={{ height: 30, marginTop: 4 }} /&gt;
      &lt;/Link&gt;

      &lt;Button to=&quot;/post&quot;&gt;board&lt;/Button&gt;
      &lt;Button to=&quot;/mypage&quot;&gt;mypage&lt;/Button&gt;

      {user ? (
        &lt;&gt;
          &lt;Button&gt;{user}&lt;/Button&gt;
          &lt;Button onClick={onLogout}&gt;logout&lt;/Button&gt;
        &lt;/&gt;
      ) : (
        &lt;&gt;
          &lt;Button to=&quot;/login&quot;&gt;login&lt;/Button&gt;
          &lt;Button to=&quot;/register&quot;&gt;register&lt;/Button&gt;
        &lt;/&gt;
      )}
    &lt;/SNavBar&gt;
  );
};

huangapple
  • 本文由 发表于 2023年7月23日 22:07:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定