3

I am using wagmi usePrepareContractWriteand write to write to my contract. I am allowing the user to upload fields and create an NFT. Is there a way to set the args in the write function? For example i have

  const { config, status } = usePrepareContractWrite({
    addressOrName: "0xADDRESS",
    contractInterface:  "abi file",
    functionName: "safeMint",
  });
  const { data, isLoading, isSuccess, write } = useContractWrite(config);


  write();

It works fine if i add the args:["address", "NFT data"] in the usePrepareContractWrite . However is there a way to set this in write() instead? As the NFT information is only available later after the user uploads and image and fields.

Docs: https://wagmi.sh/docs/hooks/useContractWrite

1 Answer 1

1

You can use useState for something this like this, just throwing code together didnt test....

import React, { useState } from 'react';

function Example() {
  // Declare a new state variable, which we'll call "param"
  const [param, setParam] = useState("");

  const { config, status } = usePrepareContractWrite({
    addressOrName: "0xADDRESS",
    contractInterface:  "abi file",
    functionName: "safeMint",
    args: [param]
  });
  const { data, isLoading, isSuccess, write } = useContractWrite(config);

  return (
    <div>
      <form>
       <input
          type="text"
          placeholder="parameter type here"
          name="param"
          onChange={(e) => setParam(e.target.value)}
        />
        <button onClick={() => write()}>
          Click me
        </button>
      </form>
    </div>
  );
}

https://reactjs.org/docs/hooks-state.html

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.