英文:
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 = () => {
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;
});
...
答案1
得分: 1
错误似乎来自这一行 <SError>{error && <div>{error}</div>}</SError>
error
通常是一个对象。尝试 <SError>{error && <div>{JSON.stringify(error)}</div>}</SError>
英文:
Error seems to be coming from this line <SError>{error && <div>{error}</div>}</SError>
error
is generally an object. Try <SError>{error && <div>{JSON.stringify(error)}</div>}</SError>
答案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 = () => {
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}</Button>
<Button onClick={onLogout}>logout</Button>
</>
) : (
<>
<Button to="/login">login</Button>
<Button to="/register">register</Button>
</>
)}
</SNavBar>
);
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论