react相关hooks(一)

视图上某个状态发生了变化,所以视图要进行相应的更新
vue是编译时的行为
react时运行时的行为
useReducer //搜集所有操作某一个数据的方案

const {useReducer} from react;
const [count,dispatch] = useReducer(countReducer],0)
const countReducer(count,{type, payload}){
	switch(case){
		case 'Plus':
		return count+playload;
		case 'Minus':
		return count-playload;
		default:
		return count;
	}
}
return (
	<div>
		<h1>count<h1>
		<button onClick={()=>{dispatch({type:'Plus',payload:2})}}></button>
		<h1>count<h1>
		<button onClick={()=>{dispatch({type:'Minus',payload:3})}}></button>
	</div>
)

useEffect //与视图不相关的所有逻辑都是副作用 手动收集依赖

useEffect(()=>{},[])
第二个参数undefined,任何状态,都会执行回调函数 组件更新的声明周期
第二个参数不为数组,报警告
第二个参数[],回调函数只会函数组件调用时执行一次 conpomentDidMount
第二个参数有元素的数组,元素为状态 状态更新 回调重新执行一次 conpomentDidUpdate
useEffect(()=>{
	// 页面卸载时 componentWillUnmount
	// 这里只能返回一个函数 如果写async 返回的是promise
	return ()=>{
	// 清楚副作用 比如全局的函数 定时器
	}
},[])
useState手写
const states=[]
const stateSetters = []

let stateIndex = 0;
function createState(initialState,stateIndex){
	return states[stateIndex]? states[stateIndex]:initialState;
}
function createStateSetter(stateIndex){
	return ((newState)=>{
		if(typeof newState === 'fucntion'){
			states[stateIndex] = newState(states[stateIndex])		
		}else{
			states[stateIndex] = newState
	}
	render();
	})
}
export function useState(initialState){
	states[stateIndex] = createState(initialState,stateIndex)
	if(!stateSetters[stateIndex]){
		stateSetters.push = createStateSetter(stateIndex)
	}
	const _state = states[stateIndex]
	const _setState = stateSetters[stateIndex]
	stateIndex++;
	return [_state,_setState ]
}
async function render()=>{
const App = (await import...)
stateIndex = 0;
effectIndex = 0;
root.render(<App/>)
}
//useReducer
export function useReducer(reducer,initialState){
	const [state, setState] = useState(initialState)
	function dispatch({type,payload}){
		const newSate reducer(state, {type,payload})
		setState(newState);
	}
	return [state.dispatch]
}
useEffect
fucntion useEffect(cb,depArr){
	if(typeof cb!== 'function'){
	throw new TypeError('callback must be a function')
	}
	if (depArr !== undefined && !Array.isArray(depArr){
	throw new TypeError('depArr must be an array ')
	}
	const isChange = effectDepArr[effectIndex]?depArr.some((dep,index) => dep !== effectDepArr[effectIndex][index]):true;
	if(isChange || depArr=== undefined){
		cb();
	}
	effectDepArr[effectIndex] = depArr;
	effectIndex++;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值