This is a case about React and Dapp.
I call web3j to sendTransaction and wait callback by chian fucntion. While error occur , I can't call the function inside App.
I descibe the situations below codes around error block.
Is it possible to call function handleError?
//My code
class App extends Component {
constructor(props) {
super(props);
this.state = {
status: ''
};
};
startTrade(){
web3.eth.sendTransaction({
from: "0x123....",
to: "0x456....",
value: web3.utils.toWei("2", "ether"),
}).on('error', (error)=>{
//If write this line,the browser shows directly:
//Failed to compile
//src\App.js
//Line 62:7: 'handleFail' is not defined no-undef
//handleError();
//If add this. when error occurs,the console shows:
//App.js:62 Uncaught TypeError: Cannot read properties of undefined (reading 'handleFail')
//this.handleFail();
//If I call otherHandle,It's work.
//However I don't have the instance of App to change something...
//otherHandle();
});
}
handleError(){
console.log("Do something..");
}
render(){ return();
}
}
function otherHandle(){
console.log("Do something..");
}
export default function App() {
const handleError = (error) => {
console.log("Do something..", error.message);
}
const startTrade = ()=>{
web3.eth.sendTransaction({
from: "0x0Cb......",
to: "0x61d63........",
value: web3.utils.toWei("2", "ether"),
}).on('error', (error)=>{
handleError(error);
});
}
// useEffect Function...
useEffect(() => {
startTrade();
// eslint-disable-next-line
}, []);
return (
<div className="App">
</div>
);
}