React native Hooks sync UseState in 2 diferent files - react-native

I want to sync the value of a useState in 2 different files from a useHook
I have a file named useChangeScreen witch I use to set when I want to show the diferent Views:
export const useChangeScreen = () => {
...
const [homeActivo, setHomeActivo] = useState(false);
const [searchActivo, setSearchActivo] = useState(true);
const [profileActivo, setProfileActivo] = useState(false);
...
const irAHome = () => {
setHomeActivo(true);
setSearchActivo(false);
setProfileActivo(false);
};
const irASearch = () => {
setHomeActivo(false);
setSearchActivo(true);
setProfileActivo(false);
};
const irAProfile = () => {
setHomeActivo(false);
setSearchActivo(false);
setProfileActivo(true);
};
...
return {
homeActivo,
searchActivo,
profileActivo,
irAHome,
irASearch,
irAProfile
}
}
This hook is called in the navigation component:
export const Nav = () => {
const {
irAHome,
irANotifi,
irAProfile,
irASearch
} = useChangeScreen();
...
return (
...
<TouchableOpacity onPress={irAHome}>
...
<TouchableOpacity onPress={irASearch}>
...
<TouchableOpacity onPress={irAProfile}>
...
)
}
and in the screen controller I have this:
export const ScreenController =() => {
const {
homeActivo,
searchActivo,
profileActivo,
} = useChangeScreen();
...
return(
...
{homeActivo ? (
<HomeScreen />
) : searchActivo ? (
<SearchShopsScreen />
) : profileActivo ? null : null}
...
)
}
when I press the buttons in the nav I want the views in ScreenController to change from Home to Profile or Search, but when I press the buttons, the state dont change

You can lift up the state to the parent component and pass it down to it's children, use React Context API or Redux.
If you chose to lift up the state:
Then you would have a parent component that looks like this:
// ...
const Parent = () => {
const {
irAHome,
irANotifi,
irAProfile,
irASearch,
homeActivo,
searchActivo,
profileActivo
} = useChangeScreen();
return (
<>
<Nav
irAHome={irAHome}
irANotifi={irANotifi}
irAProfile={irAProfile}
irASearch={irASearch}
/>
<ScreenController
homeActivo={homeActivo}
searchActivo={searchActivo}
profileActivo={profileActivo}
/>
</>
);
};
// ...
Then use the values passed from props like that:
export const ScreenController =({ homeActivo, searchActivo, profileActivo }) => {
// ...
return (
// ...
{homeActivo ? (
<HomeScreen />
) : searchActivo ? (
<SearchShopsScreen />
) : profileActivo ? null : null}
// ...
);
};
and:
export const Nav = ({
irAHome,
irANotifi,
irAProfile,
irASearch
}) => {
// ...
return (
// ...
<TouchableOpacity onPress={irAHome} />
// ...
<TouchableOpacity onPress={irASearch} />
// ...
<TouchableOpacity onPress={irAProfile} />
// ...
)
}
Note:
You should've actually used only one state which stores the current screen and checked for the current screen using comparison operators.
Checkout these for more details:
Lifting State Up
React Context API
Get Started with Redux

Related

(React Native && RTK Query) How to make sure the data has been returned when use conditional fetching

export function Login() {
const [skip, setSkip] = useState(true);
const { data, isFetching } = useVerifyUserQuery(userState, {
skip,
});
const LoginButton = () => (
<Button
title="Login"
onPress={() => {
setSkip((prev) => !prev);
}}
/>
);
return (
…
)
}
The requirement is to make a request when the button is pressed, and then store the returned data in a constant. Is there a good way to make sure data is returned before I store it.
Here is one of my solutions. Obviously it may cause some problems.
onPress={() => {
setSkip((prev) => !prev);
while(isFetching){}
// save data
}}
And with the code below, storeData will be called multiple times.
export function Login() {
const [skip, setSkip] = useState(true);
const { data, isFetching } = useVerifyUserQuery(userState, {
skip,
});
if (!isFetching && IsNotEmpty(data)){
storeData();
}
const LoginButton = () => (
<Button
title="Login"
onPress={() => {
setSkip((prev) => !prev);
}}
/>
);
return (
…
)
}
It looks like you just want to use the lazy version - useLazyVerifyUserQuery instead of common. It will be like:
export function Login() {
const [ verifyUser ] = useLazyVerifyUserQuery();
const handleLogin = async () => {
const data = await verifyUser(userState).unwrap();
// Probably you would want to use `storeData` somehow here?
}
const LoginButton = () => (
<Button
title="Login"
onPress={handleLogin}
/>
);
return (
...
)
}
PS: just a warning - using a nested component definition, like LoginButton inside Login - is a known antipattern that may cause significant performance issues.

React native app performance on connect(mapStateToProps, mapDispatchToProps)

I'm creating react native app with redux state management. I want to know what is the best practice of having connect(mapStateToProps, mapDispatchToProps).
I have several component classes i.e. ParentA, ChildA, ChildB. Currently I'm getting state properties for each parent and child classes independently.
eg:
const ParentA = (props) => {
return (
<View>
<Text>{props.item.name}</Text>
<ChildA />
<ChildB />
</View>
)
}
const mapStateToProps = (state) => {
const { item } = state
return {
item: item.item,
}
}
export default connect(mapStateToProps)(ParentA)
const ChildA = (props) => {
return (
<View>
<Text>{props.item.name}</Text>
</View>
)
}
const mapStateToProps = (state) => {
const { item } = state
return {
item: item.item,
}
}
export default connect(mapStateToProps)(ChildA)
const ChildB = (props) => {
return (
<View>
<Text>{props.item.age}</Text>
</View>
)
}
const mapStateToProps = (state) => {
const { item } = state
return {
item: item.item,
}
}
export default connect(mapStateToProps)(ChildB)
But rather having connect for each child component I could get item state from ParentA and pass it to Child components.
eg:
const ParentA = (props) => {
return (
<View>
<Text>{props.item.name}</Text>
<ChildA item={item}/>
<ChildB item={item}/>
</View>
)
}
const mapStateToProps = (state) => {
const { item } = state
return {
item: item.item,
}
}
export default connect(mapStateToProps)(ParentA)
const ChildA = (props) => {
return (
<View>
<Text>{props.item.name}</Text>
</View>
)
}
const mapStateToProps = (state) => {
const { item } = state
return {
item: item.item,
}
}
export default ChildA
const ChildB = (props) => {
return (
<View>
<Text>{props.item.age}</Text>
</View>
)
}
const mapStateToProps = (state) => {
const { item } = state
return {
item: item.item,
}
}
export default ChildB
My questions are,
What would be the best approach while considering the app performance ?
Can I use same approach for mapDispatchToProps as well ?
Yes, you can Use useSelector, useDispatch but the thing is you should use hooks. It can fix considering the app performance with this approach.
I think rather than using 'const', try another datatypes such as 'var' or 'let' as 'const' value once fixed cannot be changed.

Iterate over values of Map to render all icon components but don't work however render one icon works

I am developing react-native project.
I have a function which set icon metadata into a Map :
export function getIconsMetadata() {
// a map of icons' metadata
const iconsMetadata = new Map();
...
// code to set icon metadata to the map
iconsMetadata.set("foo", "Foo");
iconsMetadata.set("bar", "Bar");
...
return iconsMetadata;
}
There is another function which returns the actual icon component based on the icon type (i.e. the value of iconsMetadata holds the icon type):
export function getMyIcon(iconType) {
switch (iconType) {
case 'Foo':
return <Foo />;
case 'Bar':
return <Bar />;
...
}
In my screen, I have a function to show icon component by iterating over the values of the above icons' metadata Map, and try to render each icon component:
export const MyScreen() => {
const showIcons = () => {
[...getIconsMetadata().values()].map((iconType, index) => {
const iconComponent = getMyIcon(iconType);
return <View key={index}>
{iconComponent}
</View>;
});
};
return (
<View style={styles.container}>
{/*I call the showIcons function here to render icons*/}
{showIcons()}
</View>
)
}
Problem is the icons are not shown on screen.
But if I directly return one icon component in my screen:
export const MyScreen = () => {
...
const showOneIcon = () => {
return <View>
<Foo />
</View>;
});
}
return (
<View style={styles.container}>
{/*I show one icon*/}
{showOneIcon()}
</View>
)
}
The <Foo /> icon component is rendered successfully on the screen.
So, why iterating the map to show all icons don't work?
The problem is that you’re not returning anything from showIcons. Either you remove { } from there
const showIcons = () =>
[...getIconsMetadata().values()].map((iconType, index) => {
const iconComponent = getMyIcon(iconType);
return <View key={index}>{iconComponent}</View>;
});
or add return before [...getIconsMetadata().values()].map
const showIcons = () => {
return [...getIconsMetadata().values()].map((iconType, index) => {
const iconComponent = getMyIcon(iconType);
return <View key={index}>{iconComponent}</View>;
});
};

How to re render react hook function component when redux store change?

I have a function component UpdateCustomerScreen connect with redux store and use react-navigation navigate to SelectorGenderScreen.
selectedCustomer is my redux store data. I change the data on SelectorGenderScreen, when I use navigation.pop() to UpdateCustomerScreen. I have no idea how to re render the UpdateCustomerScreen.
Here is my UpdateCustomerScreen:
const UpdateCustomerScreen = ({ navigation, selectedCustomer }) => {
const gender = changeGenderOption(selectedCustomer.sex); // gender is an array.
const [sex, setSex] = useState(gender); // set array to my state.
console.log('sex', sex);
return (
<View>
<TouchableOpacity onPress={() => navigation.push('SelectorGenderScreen')}
<Text>Navigate to next screen</Text>
</TouchableOpacity>
</View>
);
const mapStateToProps = (state) => {
const { selectedCustomer } = state.CustomerRedux;
return { selectedCustomer };
};
export default connect(mapStateToProps, {})(UpdateCustomerScreen);
Here is my SelectorGenderScreen:
const SelectorGenderScreen = ({ navigation, selectedCustomer, changeGender }) => {
const gender = changeGenderOption(selectedCustomer.sex);
const [genderOption, setGenderOption] = useState(gender);
return (
<Header
title={Translate.chooseStore}
leftComponent={
<BackButton onPress={() => navigation.pop()} />
}
/>
<TouchableOpacity onPress={() => changeGender(selectedCustomer, genderOption)}>
<Text>Change the redux store data</Text>
</TouchableOpacity>
);
const mapStateToProps = (state) => {
const { selectedCustomer } = state.CustomerRedux;
return { selectedCustomer };
};
const mapDispatchToProps = dispatch => {
return {
changeGender: (selectedCustomer, genderOption) => {
dispatch(changeGender(selectedCustomer, genderOption));
}
};
};
export default connect(mapStateToProps, mapDispatchToProps)(SelectorGenderScreen);
I try to use useCallback() in UpdateCustomerScreen. When I navigation.pop(), It still doesn't re render.
// my state
const [sex, setSex] = useState(gender);
// It is not working
useCallback(() => {
console.log(sex);
},[sex]);
// It is not working
console.log('sex', sex);
return (
// my view
);
Any way to re render the UpdateCustomerScreen when redux store value has been changed ?

jest.spyOn not called

I'm trying to test this component using jest and enzyme in react native:
export class MyItem extends PureComponent {
_onDelete = () => {
console.log('Deleting !')
}
_renderSwpItem = () => {
return (
<TouchableOpacity id="delete" onPress={this._onDelete}>
<Icon name="delete" size={50} color={Colors.white}/>
</TouchableOpacity>
)
}
...
}
So far I wrote:
describe('Testing MyItem', () => {
const wrapper = shallow( <MyItem />)
it('_renderSwptItem press works', () => {
const instance = wrapper.instance()
const swpWrapper = shallow(instance._renderSwpItem())
const deletePressSpy = jest.spyOn(instance, '_onDelete')
swpWrapper.find('#delete').simulate('press')
expect(deletePressSpy).toHaveBeenCalledTimes(1)
})
})
I can see in the log Deleting ! but expect(deletePressSpy).toHaveBeenCalledTimes(1) fails. What am I doing wrong ?
It seems that calling spyOn before calling shallow(instance._renderSwpItem()) does the trick. Not sure why thought...