- import React, { memo, useRef, forwardRef, useImperativeHandle } from 'react'
-
- const HelloWorld = memo(forwardRef((props, ref) => {
-
- const inputRef = useRef()
-
- // 子组件对父组件传入的ref进行处理
- useImperativeHandle(ref, () => {
- return {
- focus() {
- console.log("focus")
- inputRef.current.focus()
- },
- setValue(value) {
- inputRef.current.value = value
- }
- }
- })
-
- return <input type="text" ref={inputRef}/>
- }))
-
-
- const App = memo(() => {
- const titleRef = useRef()
- const inputRef = useRef()
-
- function handleDOM() {
- // console.log(inputRef.current)
- inputRef.current.focus()
- // inputRef.current.value = ""
- inputRef.current.setValue("哈哈哈")
- }
-
- return (
- <div>
- <h2 ref={titleRef}>哈哈哈h2>
- <HelloWorld ref={inputRef}/>
- <button onClick={handleDOM}>DOM操作button>
- div>
- )
- })
-
- export default App
useLayoutEffect看起来和useEffect非常的相似,事实上他们也只有一点区别而已:
如果我们希望在某些操作发生之后再更新DOM,那么应该将这个操作放到useLayoutEffect。
Redux7.1开始,提供了Hook的方式,我们再也不需要编写connect以及对应的映射函数
useSelector的作用是将state映射到组件中:
- import React, { memo } from 'react'
- import { useSelector, useDispatch, shallowEqual } from "react-redux"
- import { addNumberAction, changeMessageAction, subNumberAction } from './store/modules/counter'
-
-
- // memo高阶组件包裹起来的组件有对应的特点: 只有props发生改变时, 才会重新渲染
- const Home = memo((props) => {
- const { message } = useSelector((state) => ({
- message: state.counter.message
- }), shallowEqual) // shallowEqual优化,只有当组件内使用的state.message改变时重新加载 不会由于父组件更新了state的其他参数重新渲染
-
- const dispatch = useDispatch()
- function changeMessageHandle() {
- dispatch(changeMessageAction("你好啊, 师姐!"))
- }
-
- console.log("Home render")
-
- return (
- <div>
- <h2>Home: {message}h2>
- <button onClick={e => changeMessageHandle()}>修改messagebutton>
- div>
- )
- })
-
-
- const App = memo((props) => {
- // 1.使用useSelector将redux中store的数据映射到组件内
- const { count } = useSelector((state) => ({
- count: state.counter.count
- }), shallowEqual)
-
- // 2.使用dispatch直接派发action
- const dispatch = useDispatch()
- function addNumberHandle(num, isAdd = true) {
- if (isAdd) {
- dispatch(addNumberAction(num))
- } else {
- dispatch(subNumberAction(num))
- }
- }
-
- console.log("App render")
-
- return (
- <div>
- <h2>当前计数: {count}h2>
- <button onClick={e => addNumberHandle(1)}>+1button>
- <button onClick={e => addNumberHandle(6)}>+6button>
- <button onClick={e => addNumberHandle(6, false)}>-6button>
-
- <Home/>
- div>
- )
- })
-
- export default App
- import React, { memo, useState, useTransition } from 'react'
- import namesArray from './namesArray'
-
- const App = memo(() => {
- const [showNames, setShowNames] = useState(namesArray)
- const [ pending, startTransition ] = useTransition()
-
- function valueChangeHandle(event) {
- startTransition(() => {
- const keyword = event.target.value
- const filterShowNames = namesArray.filter(item => item.includes(keyword))
- setShowNames(filterShowNames)
- })
- }
-
- return (
- <div>
- <input type="text" onInput={valueChangeHandle}/>
- <h2>用户名列表: {pending && <span>data loadingspan>} h2>
- <ul>
- {
- showNames.map((item, index) => {
- return <li key={index}>{item}li>
- })
- }
- ul>
- div>
- )
- })
-
- export default App