Thank you! Your example did not work right away, but it put me on the right track!
My example might have been misleading because I used t
, which might have a special meaning in Reason (I’m just starting with this new language an don’t know all the idioms yet). In my original file, I called t
credentialsPair
instead. So this is what I ended up with:
// Credentials.re
type credentialsPair = { user: string, password: string };
let getCredentialsPair = ( user: string, password: string ): credentialsPair = {user, password}
// Component.re
let credentials = React.useState(() => Credentials.getCredentialsPair("", ""));
let changeCredentials = (user: string, password: string) => {
setState( _prevState => Credentials.getCredentialsPair(user, password) );
}
I did not know the syntax of calling setState
with a function instead of a record, this was the part of your example that helped me. The error message I got originally on setState
was
This expression has type Credentials.credentialsPair = Credentials.credentialsPair but an expression was expected of type Credentials.credentialsPair => Credentials.credentialsPair
Is this a general rule or pattern in ReasonReact that if you use a record type with React.useState
you have to call the callback with a function instead of the record value?
Side note: In other places of my application I’ve used the React.useReducer
instead (which would also have been a better fit for my original example). In this particular case, I’m always switching out the whole record, so I thought React.useState
would be more appropriate.