語法:const [n, setN] = React.useState(0);
ide
0是n的默認值,setN是操做n的函數
setN:函數
useState:code
x:作用域
語法:io
const nRef = React.useRef(0); nRef.current(表示當前的值)
nRef.current:function
const themeContext = React.createContext(null); function App() { const [theme, setTheme] = React.useState("red"); return ( // themeContext.Provider 建立一個局部做用域 // 其中裏面的全部組件均可以使用setTheme這個函數 <themeContext.Provider value={{ theme, setTheme }}> <div className={`App ${theme}`}> <p>{theme}</p> <div> <ChildA /> </div> <div> <ChildB /> </div> </div> </themeContext.Provider> ); } function ChildA() { // ChildA這個子組件內部想使用父組件setTheme函數的方式 const { setTheme } = React.useContext(themeContext); return ( <div> <button onClick={() => setTheme("red")}>red</button> </div> ); }