0

i have to make a few changes in a project and i'm not really familiar with react. How can i change the state of a Hook from an outside function? This is what i have:

export const HomeView = () => {

[...]

const ProductDetails = () => {
  const [number, setNumber] = React.useState(0);
  const [currentPrice, setCurrentPrice] = React.useState(5);

  React.useEffect(() => {
    const fetchData = async () => {


      // get available items and current price when page loads
      [...]
      const number = 15;

      setNumber(number);

      [...]

      const currentPrice = 10;

      setCurrentPrice(currentPrice);
    
  };
  fetchData();
}, []);

  // returns available items and current price with a button to buy

     return (
         <div >
         <h2>Current price : {currentPrice} </h2>
         <button onClick={() => buyProduct({currentPrice})}>
           "Click me"
         </button>
         <h2>Available {number} / 10000</h2>
       </div>
     );


const buyProduct = async ({currentPrice}: {currentPrice: number;}) => {

  //users buys product, refresh available number
  [...]

  const number = 14;

  //each time a buy occurs, i have to increase price

  const currentPrice = 11;

  //How do i pass this new data to update the data in ProductDetails when a buy occurs without 
  //refreshing the whole page from the browser?

};

return (
  <div >
    [...]
    <ProductDetails />
    [...]
  </div>
);

I thought about moving the hook from ProductDetails inside the whole HomeView, but i don't really know how the whole process will work since i'm not that familiar with react. Thanks in advance for your help.

1 Answer 1

1

I believe that is not recommended. https://reactjs.org/docs/hooks-rules.html#only-call-hooks-from-react-functions

Only Call Hooks from React Functions Don’t call Hooks from regular JavaScript functions. Instead, you can:

  • Call Hooks from React function components.
  • Call Hooks from custom Hooks

By following this rule, you ensure that all stateful logic in a component is clearly visible from its source code.

However, you can pass an internal function as a parameter which will update the state to the method buyProduct which will update the state number and currentPrice using setNumber & setCurrentPrice respectively and call it within buyProduct itself.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey thanks for your response, tried your solution but i can't seem to implement it. Mind sharing some code? Thanks
Hey, @CosminVacaru Sorry for the delayed response. Have you fixed the problem. I think you can easily create an arrow function as follows : const buyProduct = ({{currentPrice}: {currentPrice: number;}})=>{ //remaining code } Please let me know how it goes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.