2
Hello, I’m a little new to the typescript world and there’s something going on that I can’t explain why.
I have the following state:
const [estaExecutando, mudarExecucao] = useState(false);
Step to my component as follows:
<Contador
tempo_total={tempoTotal}
estaExecutando={estaExecutando}
mudarExecucao={mudarExecucao}
/>
So within that component I have the following interface and implementation:
interface ContadorProps {
tempo_total: number;
estaExecutando: Boolean;
mudarExecucao: (prev: boolean) => boolean; // Vai receber no prev o estado anterior que no caso seria true ou false e vai retornar também true ou false
}
const Contador: React.FC<ContadorProps> = ({ tempo_total, estaExecutando, mudarExecucao }) => {
<TouchableOpacity onPress={() => mudarExecucao((prev: boolean) => !prev)}>
Play/pause
</TouchableOpacity>
}
That gives me the following mistake:
Argument of type '(Prev: Boolean) => Boolean' is not Assignable to Parameter of type 'Boolean'.
I have tried to put in the interface that this function returns void. I’ve also tried to get Boolean out of the EV
<TouchableOpacity onPress={() => mudarExecucao((prev) => !prev)}>
But still the same mistake. What am I doing wrong?
It worked perfectly, it was worth a lot! I didn’t know I needed a specific type for this. Has other React functions that need specific typing as well?
– Ban
would have to consult the documentation to see the type of each action of a React hook, but I believe each one has a specific type for each action.
– Cmte Cardeal