25
loading...
This website collects cookies to deliver better user experience
useState()
hook, you will use Jotai in a natural way.npm i react-router-dom jotai
// @src/App.jsx
import React from "react";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import { Home, Profile } from "./pages";
const App = () => {
return (
<Router>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/profile" component={Profile} />
</Switch>
</Router>
);
};
export default App;
pages
folder. Let's start by working on the Home page:// @src/pages/Home.jsx
import React from "react";
const Home = () => {
return (
<>
<h2>Lets Get Started</h2>
<form>
<input
placeholder="romaji"
name="romaji"
type="text"
required
/>
<input
placeholder="format"
name="format"
type="text"
required
/>
<button type="submit">Register</button>
</form>
</>
);
};
export default Home;
atom()
function so we can store the form data. And basically atoms hold our source of truth for our application, being exported individually and must hold an initial value.// @src/store.js
import { atom } from "jotai";
export const manhwaAtom = atom({
romaji: "",
format: "",
});
useAtom()
hook so we can read and mutate our atom. Then we also import our manhwaAtom from our store.// @src/pages/Home.jsx
import React from "react";
import { useAtom } from "jotai";
import { manhwaAtom } from "../store";
const Home = () => {
const [state, setState] = useAtom(manhwaAtom);
return (
// Hidden for simplicity
);
};
export default Home;
useState()
hook. But of course using Jotai.// @src/pages/Home.jsx
import React from "react";
import { useAtom } from "jotai";
import { manhwaAtom } from "../store";
const Home = () => {
const [state, setState] = useAtom(manhwaAtom);
const handleOnChange = (e) => {
const { name, value } = e.target;
setState({ ...state, [name]: value });
};
const handleOnSubmit = (e) => {
e.preventDefault();
};
return (
<>
<h2>Lets Get Started</h2>
<form onSubmit={handleOnSubmit}>
<input
placeholder="romaji"
name="romaji"
type="text"
value={state.romaji}
onChange={handleOnChange}
required
/>
<input
placeholder="format"
name="format"
type="text"
value={state.format}
onChange={handleOnChange}
required
/>
<button type="submit">Register</button>
</form>
</>
);
};
export default Home;
useHistory()
hook.// @src/pages/Home.jsx
import React from "react";
import { useAtom } from "jotai";
import { useHistory } from "react-router-dom";
import { manhwaAtom } from "../store";
const Home = () => {
const { push } = useHistory();
const [state, setState] = useAtom(manhwaAtom);
const handleOnChange = (e) => {
const { name, value } = e.target;
setState({ ...state, [name]: value });
};
const handleOnSubmit = (e) => {
e.preventDefault();
push("/profile");
};
return (
<>
<h2>Lets Get Started</h2>
<form onSubmit={handleOnSubmit}>
<input
placeholder="romaji"
name="romaji"
type="text"
value={state.romaji}
onChange={handleOnChange}
required
/>
<input
placeholder="format"
name="format"
type="text"
value={state.format}
onChange={handleOnChange}
required
/>
<button type="submit">Register</button>
</form>
</>
);
};
export default Home;
// @src/pages/Profile.jsx
import React from "react";
import { useAtom } from "jotai";
import { useHistory } from "react-router";
import { manhwaAtom } from "../store";
const Profile = () => {
const { push } = useHistory();
const [state, setState] = useAtom(manhwaAtom);
const handleReset = (e) => {
e.preventDefault();
setState({ romaji: "", format: "" });
push("/");
};
return (
<>
<img src="https://bit.ly/3AfK4Qq" alt="anime gif" />
<h2>
<code>{JSON.stringify(state, null, "\t")}</code>
</h2>
<button onClick={handleReset}>Reset</button>
</>
);
};
export default Profile;
App.jsx
. Like this:// @src/pages/index.js
export { default as Home } from "./Home";
export { default as Profile } from "./Profile";