I'm not able to pass the state value to the method or function in React [closed]

97
January 27, 2021, at 09:10 AM
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 21 hours ago.

Improve this question
//App.js
import "./styles.css";
import { User } from "./Components/User";
export default function App() {
  return <User />;
}
//User.js
import { useState } from "react";
import { Pagination } from "antd";
export function fetchDataAction(page) {
  console.log(page);
  return page;
}
const User = () => {
  const [pages, setPages] = useState(0);
  fetchDataAction(pages);
  function onPageChange(page) {
    setPages(page - 1);
  }
  return (
    <Pagination
      style={{ float: "right", marginTop: 20 }}
      onChange={onPageChange}
      total={500}
    />
  );
};
export default User;

fetchDataAction is one of the function where I have passed the state value but not able to read it into function definition.......

Answer 1

I think you don't need to pass it explicitly if it is within the scope of functional component you can simply use it like this.

const Parent = () => {
  const [counter, setCounter] = useState(0);
  const onClickHandler = () => {
    console.log(`counter = ${counter}`);
  }
 
  return <Child onClick={onClickHandler} />;
}

Hopefully it may help

Happy coding

READ ALSO
How to access array index of Cloud Firestore using query in Flutter?

How to access array index of Cloud Firestore using query in Flutter?

I have field users in document and this field contains two element in arrayI have to check specific two values are in this array

97
Getting an unwanted space between Nav Items in Navbar Angular

Getting an unwanted space between Nav Items in Navbar Angular

I am trying to create a sidenav bar with nav items in it which has sub menusI am able to create it but i am getting space between 2 navitems which have submenus

48
PHP Script INSERT INTO SELECT does not insert, with no errors [closed]

PHP Script INSERT INTO SELECT does not insert, with no errors [closed]

Want to improve this question? Update the question so it's on-topic for Stack Overflow

60
SQL query to select students who have taken all subjects from subjects table

SQL query to select students who have taken all subjects from subjects table

I have two tables, student_records and subjectsI have to find out those students who have taken all the subjects (1,2,3) from the subjects table

53