相关疑难解决方法(0)

如何使用 useReducer([state,dispatch]) 和 useContext 避免无用的重新渲染?

当使用多个 useReducers 时,使用状态的一部分的每个组件都会重新渲染。

import React, { useContext } from 'react'
import Store from '../store'
import { setName } from "../actions/nameActions"

const Name = () => {
    const { state: { nameReducer: { name } }, dispatch } = useContext(Store)
    const handleInput = ({ target: { value } }) => { dispatch(setName(value)) }
    console.log('useless rerender if other part (not name) of state is changed'); 
    return <div>
        <p>{name}</p>
        <input value={name} onChange={handleInput} />
    </div>
}

export default Name;
Run Code Online (Sandbox Code Playgroud)

如何避免这种无用的重新渲染?

reactjs react-hooks

7
推荐指数
1
解决办法
6044
查看次数

标签 统计

react-hooks ×1

reactjs ×1