我使用钩子将类组件转换为函数组件。目前,我正在努力弄清楚为什么这个地图中的复选框没有更新检查值,尽管 onChange 处理程序触发,并根据需要更新数组。(onSubmit 也可以正常工作,并正确更新数据库中的值)。
import {
Container,
Typography,
Grid,
Checkbox,
FormControlLabel,
Button
} from "@material-ui/core";
import Select from "react-select";
import localeSelect from "../services/localeSelect";
import {
linkCharactersToGame,
characterLinked,
linkCharacters
} from "../data/locales";
import dbLocale from "../services/dbLocale";
import { LanguageContext } from "../contexts/LanguageContext";
import { UserContext } from "../contexts/UserContext";
import { GameContext } from "../contexts/GameContext";
import { CharacterContext } from "../contexts/CharacterContext";
import { Redirect } from "react-router-dom";
export default function LinkCharacter() {
const { language } = useContext(LanguageContext);
const { user } = useContext(UserContext);
const { games, loading, error, success, connectCharacters } = useContext(
GameContext
);
const { characters } = useContext(CharacterContext);
const [game, setGame] = useState("");
const [selectedCharacters, setSelectedCharacters] = useState([]);
if (!user) {
return <Redirect to="/" />;
}
return (
<section className="link-character">
<Container maxWidth="sm">
<Typography variant="h5">
{localeSelect(language, linkCharactersToGame)}
</Typography>
{error && (
<p className="error">
<span>Error:</span> {error}
</p>
)}
{success && <p>{localeSelect(language, characterLinked)}</p>}
<Select
options={games.map(game => {
return {
label: dbLocale(language, game),
value: game._id
};
})}
我觉得我在 Hooks 中遗漏了一些东西(或者 Hooks 处理类似这样的事情存在某种问题)。我一直在四处寻找和询问,但也没有其他人能够弄清楚这个问题。
千万里不及你
相关分类