Reputation: 185
I Created a Submit()
function in one component with name formSignup.js
and I created another component with name buttonSubmit.js
that contain Button(onPress) with that button I want to call Submit()
function that is in formSignup.js
.
Reason for creating separate component for Submit button is that, I wrote a lot of code for animation and also I want to re-use that Button in loginComponent.js
.
formSignup.js
submit = () => {
...
}
buttonSubmit.js
How can i call submit function from fromSignup.js here
_onPress() {
if (this.state.isLoading ) return;
this.setState({isLoading: true});
Animated.timing(this.buttonAnimated, {
toValue: 1,
duration: 200,
easing: Easing.linear,
}).start();
setTimeout(() => {
this._onGrow();
}, 2000);
setTimeout(() => {
Actions.mainScreen();
this.setState({isLoading: false});
this.buttonAnimated.setValue(0);
this.growAnimated.setValue(0);
}, 2300);
}
-----
return(
<TouchableOpacity
onPress={this._onPress}
activeOpacity={1}>
{this.state.isLoading ? (
<Image source={spinner} style={styles.image} />
) : (
<Text style={styles.text}>Go</Text>
)}
</TouchableOpacity>
);
Upvotes: 0
Views: 633
Reputation: 701
In your case, I have a doubt.
Are you using buttonSubmit.js component inside formSignup.js component?
if yes, you can achieve it by passing the function as a props.
//FormSignup.js
const FormSignup = () =>{
const submit = () => {
// some submit logic
};
//render
return (
<View>
....
// if you are using the ButtonSubmit component in FormSignup.js, you
// can get function by props
<ButtonSubmit onPressButton = {()=>submit()}/>
</View>
)
}
//ButtonSubmit.js
const ButtonSubmit = (props) => {
return (
<TouchableOpacity onPress={props.onPressButton}>
<Text>submit</Text>
</TouchableOpacity>
)
}
if No, you should write that function as separately and import it in ButtonSubmit.js component
// Utility.js
const submit = () => {
// submit logic
};
export {submit};
//================================//
//ButtonSubmit.js
import { submit } from './path/Utility.js' // import the submit method
const ButtonSubmit = () => {
return (
<TouchableOpacity onPress={()=>submit()}>
<Text>submit</Text>
</TouchableOpacity>
)
}
Upvotes: 2
Reputation: 694
You can pass the function as props
<buttonSubmit onSubmit={this.submit} />
And inside your buttonSubmit.js you call it from props wherever you want.
//inside somewhere inside buttonSubmit you call:
this.props.onSubmit();
Upvotes: 0