如何从React上下文更新React组件
如何通过我需要根据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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它没有更新,因为您使用的是错误的。在这里,您可以在这里学习有关 hooks 的所有需要。上下文挂钩应在您的组件内部使用:这
是答案有关如何从其消费者中更新数据
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:
And here is the answer on how to update data in context from its consumer