i have been trying to create a simple posting mechanism like a personal note taker. something like twitter posting. so while i am doing coding in react i get this unusual bug where i get my current state after TWO clicks. this is frustrating as i have been rereading the code for over an hour and i dont think i did anything wrong. some posts suggest using useffect because usestate is asychronous and will not update the state immediatly . how do i fix this i have attached my code below.
function Poster() {
const tw = useRef('')
const [post,setPost] = useState([])
function PostNow(event){
setPost((oldpost) => [tw.current.value,...oldpost]);
return console.log(post)
}
return (
<div >
<textarea ref={tw} ></textarea>
<button className="postIt" onClick={PostNow}>press me</button>
</div>
)
}
export default Poster