In my React Native 0.70 app, there are 2 components Home
(parent) and ListSearch
(child). Users enter server string in Home
and search result is displayed in ListSearch
. When users click navigation.goBack()
on ListSearch
to go back to Home
, useFocusEffect
from react navigation 6.x is used to reset the placeholder on search bar in Home
. Here is the code in Home
(parent) to reset the placeholder:
export default Home = ({ navigation}) => {
const searchHolder = "Enter search string here";
const [plcholder, setPlcholder] = useState(searchHolder);
const submitsearch = async () => {
...
setPlcholder(searchHolder);//reset place holder
navigation.navigate("ListSearch", {artworks:res, title:"Search Result"}). //<<==ListSearch is component of result displaying
}
//reset placeholder whenever the Home is focused.
useFocusEffect(
React.useCallback(() => {
setPlcholder(searchHolder); // reset the place holder search bar
},[navigation])
);
//view
return (
...
<View style={{flex:5}}>
<TextInput style={{fontSize:hp("3%")}} placeholder={plcholder} onChangeText={strChg}></TextInput>. //plcholder here
</View>
)
}
The code above didn't work. When users navigation.goBack()
to Home
component, the placeholder in search bar was the previous search string and was not updated.