Posts > create your own useState

create your own useState

Creating a useState method using vanilla javascript.

function stringState(initial) {
	let initialState = initial;
	let newValue;
	return [
		() => initialState,
		function newValue(v){
			initialState = v;
		}
	]
}

const [strGetter, strSetter] = stringState('boop');
console.log(strGetter());
strSetter('beep');
console.log(strGetter());

Tags 🏷