Pass Context between Siblings Using Context in React

export const NumberContext = React.createContext(); function App() { const [foo, changeFoo] = useState('foo') return ( <NumberContext.Provider value={{ foo, changeFoo }}> <TheButton /> <Display /> </NumberContext.Provider> ); } function TheButton() { const { changeFoo } = useContext(NumberContext) return ( <button onClick={() => changeFoo('bar')}>Click me</button> ); } function Display() { const context Read more…

The useState asynchronous Callback in React Hook can’t Get the Latest Value and the Solution

First of all, setState has two ways of passing parameters: Pass the new value directly: setState(options) const [state, setState] = useState(0); setState(state + 1); Pass in the callback function: setState(callBack) const [state, setState] = useState(0); setState((prevState) => prevState + 1); // prevState is the state value before the change, // Read more…

React Markdown(II) – auto generate catalog, support math formula and customize footnotes

Last time, we have introduced how to display a Markdown document in React.(http://52.65.48.177/index.php/2023/03/13/rendering-markdown-in-react/) Now, let’s move forward to support ToC(Table of Content), Mathematical Formula and Footnote. 1. ToC (Table of Content) To support ToC and in-page jump, we need two plugins: remarkToc and rehypeSlug. RehypeSlug is a plugin to add Read more…

Check Form Validation with Yup and Formik

import React, {useEffect} from "react"; import { useFormik } from "formik"; import { Box, Button, FormControl, FormErrorMessage, FormLabel, Heading, Input, Select, Textarea, VStack, } from "@chakra-ui/react"; import * as Yup from 'yup'; import FullScreenSection from "./FullScreenSection"; import useSubmit from "../hooks/useSubmit"; import {useAlertContext} from "../context/alertContext"; const LandingSection = () => { Read more…