Introduction
I had this code:
import React, { Component } from 'react';
import { View, Button } from 'react-native';
export default function App() {
const myMethod = (name) => {
console.log(`Hello ${name}!`);
};
return (
<View>
<Button onPress={() => myMethod("Victor")}>
Press me!
</Button>
</View>
);
}
Then, I decided to refactor it using curried functions. Like this:
import React, { Component } from 'react';
import { View, Button } from 'react-native';
export default function App() {
/**
* @param {string} name - The name.
* @returns {Function} Curried function.
*/
const myMethod = (name) => {
return () => {
console.log(`Hello ${name}!`);
}
};
return (
<View>
<Button onPress={myMethod("Victor")}>
Press me!
</Button>
</View>
);
}
As you can see, the way I am invoking the method has changed, as I am returning a function instead of void in myMethod. That's cool, it works... every time the button is pressed, my method is invoked.
Check this snack: https://snack.expo.dev/Rx3iBVT_W
Problem
Now, I am trying to refactor the refactored code using curry from Lodash.
This is what I have tried:
import React, { Component } from 'react';
import { View, Button } from 'react-native';
import { curry } from 'lodash';
export default function App() {
const myMethod = curry((name) => {
console.log(`Hello ${name}!`);
});
return (
<View>
<Button onPress={myMethod("Victor")}>
Press me!
</Button>
</View>
);
}
But for some reason, this is not working. The method is not executed every time the button is pressed... Instead, it is invoked when the component mounts.
How can I solve it?
Check this snack: https://snack.expo.dev/bna6cLkhp