Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

.push( ) is not updating the variable

const handlePress=(day)=>{
   setData(day); //`day` is set here but not applied immediately
   setName(`${events[rand]}`)
   datas.push(data); //data is the previous data, not `day`
   console.log(datas);
 }
Comment

.push( ) is not updating the variable

const datas=[]; //move your `datas` to the global scope

export default function App() {
 const [data,setData]= useState([]);
 const [activity, setActivity]= useState([])
 const [name, setName] = useState("")

 
 useEffect(()=>{
  // handlePress()
 }, [setData, setName])

 const rand= Math.floor(Math.random(1,5)*4)+1
 const events=["Event A","Event B","Event C","Event D","Event E"]

 const handlePress=(day)=>{
   setData(day); //`data` will be changed later
   setName(`${events[rand]}`)
   datas.push(day); //push `day` directly instead of data
   console.log(datas);
 }
  return (
    <div className="App">
      <Calendar 
        onDayPress={day => {
          {handlePress(day)}
        }}
        
      />
      
    </div>
  );
}
Comment

.push( ) is not updating the variable

//whenever `name` or `data` change, `useEffect` will be triggered
useEffect(()=>{
  //TODO: Do your logic here
 }, [data, name])
Comment

PREVIOUS NEXT
Code Example
Javascript :: context Menus 
Javascript :: socket io check send 
Javascript :: Extract and convert from JSON by Regex 
Javascript :: supertest npm send headers node js 
Javascript :: chain underscore 
Javascript :: new Date() how can i ue 
Javascript :: setup app files in node js 
Javascript :: Any array in JSON object is not empty 
Javascript :: Connect session middleware - regenerate vs reload 
Javascript :: mongodb create index json 
Javascript :: open 2 links with one click html jquery 
Javascript :: show code in console very good 
Javascript :: Printer Errors 
Javascript :: Importing Ky Module In JavaScript 
Javascript :: phaser time event start at 
Javascript :: jquery show loader 
Javascript :: Declare Function In Class Constructor 
Javascript :: underscore js 
Javascript :: append vs appendchild 
Javascript :: json whitespace code 
Javascript :: change dir npm run build 
Javascript :: jquery properties 
Javascript :: how to check bot channel permissions 
Javascript :: camelcase to css variable javascript 
Javascript :: react : calling APIs after render w error message 
Javascript :: base64-XMLHttpRequest 
Javascript :: Solution-1-Part-B--solution options for reverse bits algorithm js 
Javascript :: javascript tree search 
Javascript :: jquery textarea value 
Javascript :: js DFS 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =