Update and reload component in react native - react-native

I am trying to refresh/ reload the page with an updated document once I click on a button. saveAnnotations(); saves the changes made to the document on the device and also updates the document with any other updates from other devices in the database.
The filepath is a REST URL to get the document.
After I click the sync button, the edits made to the document are saved but I want to reload the page so that it pulls the latest updated document from the database in the onclick button itself.
When I go back and return on this page I am able to see the lastest changes made but I want the document to reload when I click on sync button.
I tried using window.location.reload(false); but it closes the page where as I just want to reload the page or rerender filepath props to pull latest changes when I click on the sync button.
Any suggestions on how do I achieve that?
here's my code snippet
reRender = () => {
// calling the forceUpdate() method
this.forceUpdate();
window.location.reload(false);
};
render() {
return (
<View style={{ flex: 1 }}>
<DocumentView
document={filepath}
/>
<View style={styles.button}>
<Button
onPress={() => {
// Save Document
this.saveAnnotations();
}}
title="Sync"
/>
</View>
</View>
</View>
);
}

To achieve what you are looking for, you need to trigger the render method. Doing this will show the updated document. Here is a diagram showing the lifecycle methods of React components. As you can see, there are three ways to trigger the render method:
The component receives new props
Calling setState
Calling forceUpdate
For your use case, I would suggest adding a call to forceUpdate, like so:
<View style={styles.button}>
<Button
onPress={() => {
// Save Document
this.saveAnnotations();
this.forceUpdate(); // <-- add this
}}
title="Sync"
/>
</View>

Related

Resetting a React-Native WebView Window

I have a simple web view setup within react-native
I have nav bar at the bottom and the links open different screens with web views, however the pages save state. SO let's say I navigate to an internal page on the website (so like the about page of the website) when I go onto another screen and come back to the screen - the url state is saved. How can I make it so when I click on the nav button it always redirects to me to the initial page loaded in the web view...I've committed links etc as its sensitive data.
USE CASE:
User opens app, is greeted with a homepage - they can click links on the homepage which takes them to another part of the web page - if they navigate away from the Home Screen (within the app) then go back to the Home Screen on the app - the web view is on the last internal page they were on.
I want it so when they press the home button on the nabber on the app it effectively resets the web view?
Thanks
Code below:
const url = "websitelinkere.com/pageLink.html";
...
<View style={{ flex: 1 }}>
<WebView
javaScriptEnabled={true}
domStorageEnabled={true}
mixedContentMode={'compatibility'}
source={{
//meta type & http header locking for aded security
uri: url, //<-- I want it to always load pageLink.html <-- even if I've navigated to pageLinkDifferent.html
}}
onMessage={(event) => {}}
/>
</View>
Navbar (example link):
<View style={homePage.column}>
<TouchableOpacity onPress={() => navigation.navigate('JournalHome')}>
<Image style={homePage.navImage} source={journalHomeImage}/>
</TouchableOpacity>
</View>
I've tried researching the issue on stack overflow and online but haven't found a solution that works for me
you can use useFocusEffect in react-navigation
const INITIAL_URL = "websitelinkere.com/pageLink.html";
...
const webviewRef = useRef();
const [url,setUrl] = useState(INITIAL_URL)
...
useFocusEffect(
React.useCallback(() => {
if(webviewRef.current) {
setUrl(INITIAL_URL);
webviewRef.current.reload();
}
}, [])
);
...
<Webview
ref={webviewRef}
source={{ uri }}
/>

How to customise the UI after a user is signed in using <Authenticator> (aws amplify pre-built Auth component)

I'm trying to use aws-amplify authenticatorfor my app. My idea is to show the app first (that's the reason why i didn't wrap the whole app into withAuthenticator) and if user needs to sign in they will be led to a authentication screen, which is like below:
import { Authenticator } from 'aws-amplify-react-native'
const AuthScreen =({ route }) => {
return (
<View style={{flex: 1}}>
<Authenticator>
<TouchableOpacity
onPress={() => {
navigation.goBack()
}}
>
<Text style={{fontSize: 25}}>go back</Text>
</TouchableOpacity>
</Authenticator>
</View>
)
}
export default AuthScreen;
however in this way once the user is signed in, the UI will display like below, with a string and a signout button:
picture
My question is how can i hide this string and display the info i want after user is signed in? i tried to make a CustomSignOut component, add it in but it didn't work (didn't override):
<CustomSignOut override={'SignIn'}/>
ok finally finally figured this out... use hideDefault={true} in , then add all the other components except for Greetings (includes signout button) and TOTPSetup (somehow it just didn't work when adding it on), and the signout button & the string will disappear

How to show Modal to user before changing routes in React Navigation

So I'm wondering if I'm just not using the right vocabulary in my search - but what I thought might be cool for an app I'm working on would be:
A user starts editing their post.
In case they go back or press a tab to go to another page before
they press "Update".
The screen change is intercepted and a modal shows up and asks if
they want to keep their changes.
I know you can add event handlers - https://reactnavigation.org/docs/navigation-events/ but I'm not sure if these are what I need, I've tested and the prevent default didn't seem to do what I thought it would. That and I couldn't find a way to find what the next intended route that the app would need to go to once they have said "Dismiss" on the modal.
Could anyone point me in the right direction please?
you can create your own modal and back button so you can control what each item do
this.state={
modalConfirm: false
}
goBack = () => { //your goback function }
cancel = () => { this.setState ({modalConfirm :false})
<View>
<TouchableOpacity onPress={()=> this.setState({modalConfirm: true})>
<Icon name='arrow-left' />
</TouchableOpacity>
<Modal visible={this.state.modalConfirm}>
<Text>Do you want to go back? </Text>
<Button title='Go back' onPress={this.goBack} />
<Button title='No' onPress={this.cancel} />
</Modal>
<View>
//your content
</View>
</View>

How do I handle press and unpress touchable component with style changes?

The idea is to create a specific Touchable component acting like a button and have a feeling of pressed and unpressed, and using this component many times on my app but only one can be pressed at a time. If one is touched and then another one is touched, the first one should be unpressed and the second one should be pressed. The idea is not to use Redux to solve this problem.
I'm already handling which component was pressed, and sending through props the actions to the component. But i don't know how to manage all buttons at the same time in a generic way, by that I mean, not creating a variable to each button.
my App:
<View>
<ActivityButton activityTitle={"B1"} submit={() => this.submitHandler("B1")} />
<ActivityButton activityTitle={"B2"} submit={() => this.submitHandler("B2")} />
</View>
my Component (ActivityButton):
this.state={active:false}
<TouchableOpacity style={this.state.active ? styles.buttonPress : styles.button} onPress={this.props.submit}>
<View>
<Text>{this.props.activityTitle}</Text>
</View>
</TouchableOpacity>
I assume what you trying to do something like a Radio button groups? If your buttons only located in single page, you can achieve by using state in MyApp to check which buttons is enabled instead of button itself.
MyApp
constructor(props) {
super(props);
this.state = {
buttonIdThatEnable: "",
};
}
submitHandler = (buttonId) => {
this.setState({
buttonIdThatEnable: buttonId,
});
}
render() {
return (
<View>
<ActivityButton
activityTitle={"B1"}
active={this.state.buttonIdThatEnable === "B1"}
submit={() => this.submitHandler("B1")}
/>
<ActivityButton
activityTitle={"B2"}
active={this.state.buttonIdThatEnable === "B2"}
submit={() => this.submitHandler("B2")}
/>
</View>
)
}
ActivityButton (Use props.active to determine style)
<TouchableOpacity style={this.props.active ? styles.buttonPress : styles.button}
onPress={this.props.submit}>
<View>
<Text>{this.props.activityTitle}</Text>
</View>
</TouchableOpacity>
If your Buttons are located in different components and you do not want to use Redux, you may consider the React Context API

Disable submit button from submit in react native

I have just created a login form and doing validation on it, but not found
how to disable submit button from submit in react native if validation fails. I am not using any type of library here, just try to prevent button from submit in react native and if there is any valuable link for form handling in react native.
Any help with this would be appreciated.
You can do it in submit button's onPress function like this:
if (validation is successful)
submit
else
Do nothing
its a very simple way and other people may have different answers.
once submit, you can check if your validation is success or no and if not, just ignore or pop an alert to tell the user the error..
onSubmit = () => {
if(validationSuccess) {
//navigate to next screen component
} else {
//Alert to user
}
}
or if you do the validation onChange inside your render you still can do if else condition to make your submit button into grey and not clickable
render() {
return (
....
{this.state.validationSuccess ? (
<TouchableHighlight onPress={() => this.onSubmit()}>
<Text>LOGIN</Text>
</TouchableHighlight>
) : (
<View style={{ backgroundColor: 'grey'}}>
<Text>LOGIN</Text>
</View>
)}
....
)
}
after all, there is still other way to do this here but this is some example.. hope that helps you to start.. happy exploring!