如何从React上下文更新React组件

发布于 2025-02-12 17:03:11 字数 1406 浏览 2 评论 0原文

如何通过我需要根据contextCount.js文件上的某些事件更改React组件文件夹上的某些事件来更改计数值,

let count = 0;

 const createSale = async (url, formInputPrice, isReselling, id) => {
    // connect to smart contract

    const web3Modal = new Web3Modal();
    const connection = await web3Modal.connect();
    const provider = new ethers.providers.Web3Provider(connection);
    const signer = provider.getSigner();
    // value of wei of zeo of to convert to it that what metamask read
    const price = ethers.utils.parseUnits(formInputPrice, 'ether');
    // calling func() from contract takes time so use await
    const contract = fetchContract(signer);
    count += 1;
    const listingPrice = await contract.getListingPrice();
    const transaction = !isReselling
      ? await contract.createToken(url, price, { value: listingPrice.toString() })
      // else it belong to this
      : await contract.resellToken(id, price, { value: listingPrice.toString() });
    await transaction.wait();
  };
 <NFTContext.Provider value={{ count }}>
      {children}
    </NFTContext.Provider>

当时我需要传递计数的更新值

。在组件文件夹上使用我在其中创建了一个countupdate.jsx,从“ react”导入react;我导入上下文文件,然后使用

 const { count } = useContext(ContextCount);
const updateIt = () => (
  <div>{count}</div>
);

导出默认更新;

因此,我需要显示从ContextCount.js到组件File update.jsx的更新值。有人知道如何实现此问题吗?

How to increment a count in react context by that I need to change the count value based on some events that happens On ContextCount.js file

let count = 0;

 const createSale = async (url, formInputPrice, isReselling, id) => {
    // connect to smart contract

    const web3Modal = new Web3Modal();
    const connection = await web3Modal.connect();
    const provider = new ethers.providers.Web3Provider(connection);
    const signer = provider.getSigner();
    // value of wei of zeo of to convert to it that what metamask read
    const price = ethers.utils.parseUnits(formInputPrice, 'ether');
    // calling func() from contract takes time so use await
    const contract = fetchContract(signer);
    count += 1;
    const listingPrice = await contract.getListingPrice();
    const transaction = !isReselling
      ? await contract.createToken(url, price, { value: listingPrice.toString() })
      // else it belong to this
      : await contract.resellToken(id, price, { value: listingPrice.toString() });
    await transaction.wait();
  };
 <NFTContext.Provider value={{ count }}>
      {children}
    </NFTContext.Provider>

On React Components folder i need to pass the updated value of count when evener a createsale function is happen

So i use on components folder i created a countupdate.jsx On there import React from 'react'; i imported the context file then i use

 const { count } = useContext(ContextCount);
const updateIt = () => (
  <div>{count}</div>
);

export default updateIt;

So I need to show the updated value from ContextCount.js to Components file updateIt.jsx Does anyone know how to implement this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘过的浮云 2025-02-19 17:03:11

它没有更新,因为您使用的是错误的。在这里,您可以在这里学习有关 hooks 的所有需要​​。上下文挂钩应在您的组件内部使用:这

const updateIt = () => {
  const { count } = useContext(ContextCount);
  return (
    <div>{count}</div>
  );
};

答案有关如何从其消费者中更新数据

It is not updated because you are using the hook wrong. Here is where you can learn all you need about hooks. The context hook should be used inside your component, like this:

const updateIt = () => {
  const { count } = useContext(ContextCount);
  return (
    <div>{count}</div>
  );
};

And here is the answer on how to update data in context from its consumer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文