I am trying to make a DApp using React (just starting with it). I have my App.js
file with the tipical instructions like set the network, web3, contract status, etc.
class App extends Component {
state = { storageValue: 0, web3: null, accounts: null, contract: null };
componentDidMount = async () => {
try {
// Get network provider and web3 instance.
const web3 = await getWeb3();
// Use web3 to get the user's accounts.
const accounts = await web3.eth.getAccounts();
// Get the contract instance.
const networkId = await web3.eth.net.getId();
const deployedNetwork = SimpleStorageContract.networks[networkId];
const instance = new web3.eth.Contract(
SimpleStorageContract.abi,
deployedNetwork && deployedNetwork.address,
);
instance.options.address = "0xA65990EC0CA555d2eCDD1d84E9D1397CFA967E60"
// Set web3, accounts, and contract to the state, and then proceed with an
// example of interacting with the contract's methods.
this.setState({ web3, accounts, contract: instance }, this.runExample);
} catch (error) {
// Catch any errors for any of the above operations.
alert(
`Failed to load web3, accounts, or contract. Check console for details.`,
);
console.error(error);
}
};
runExample = async () => {
const { accounts, contract } = this.state;
// Stores a given value, 5 by default.
await contract.methods.set(25).send({ from: accounts[0] });
// Get the value from the contract to prove it worked.
const response = await contract.methods.get().call();
// Update state with the result.
this.setState({ storageValue: response });
};
render() {
if (!this.state.web3) {
return <div>Loading Web3, accounts, and contract...</div>;
}
return (
<div className="App">
<Router>
<Navbar />
</Router>
</div>
);
}
}
export default App;
Now, inside render()
function (last part of the file) I am programming my interface for the website. When I put my Navbar component there, it does not work due to hook call error. Am I using the correct place to put the Navbar component? My current Navbar is very simple
const Navbar = () => {
return (
<Nav>
</Nav>
);
};
export default Navbar;
and the style nav is declared in another js
file
export const Nav = styled.nav`
background: #000;
height: 80px;
display: flex;
justify-content: center;
align-items: center;
font-size: 1rem;
position: sticky;
top: 0;
z-index: 10;
@media screen and (max-width: 960px) {
trasition: 0.8s all ease;
}
`;
Why I cannot use const Navbar = () =>
inside the <div className="App">
? Maybe a silly question but I am not getting it.