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…

Image Height adaptive and consistent with the Width of the parent div in CSS

<div class="box"> <img src="upimg/comm.png"/> </div> <div class="box"> <img src="upimg/comm1.png"/> </div> <div class="box"> <img src="upimg/comm2.png"/> </div> .box{ width: 100%; margin: 20px auto; background: skyblue; position: relative; padding-bottom: 56%; // change this overflow: hidden; } .box>img{ width: 100%; position: absolute; top: 0; left: 0; } Note: padding-bottom attribute: when the value is 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…

MySQL Strings Segmentation

SUBSTRING_INDEX(str, delim, count) str: the string to be split. delim: the delimiter. count: the position of the delimiter. A positive number means extract from left to right, a negative number means extract from right to left. For example: SELECT SUBSTRING_INDEX('100-200-300-400', '-', 1); — output: '100' SELECT SUBSTRING_INDEX('100-200-300-400', '-', 2); — Read more…