How would I disable a function by clicking on TextInput - react-native

I have this class for blinking text:
class BlinkerFluid extends React.Component {
constructor(props) {
super(props);
this.state = {
isShowingText: true,
TextInputName = ''
};
setInterval(() => {
this.setState(previousState => {
return { isShowingText: !previousState.isShowingText };
});
}, 700);
}
render () {
let display = this.state.isShowingText ? this.props.text : ' ';
return (
<Text style={styles.other}>{display}</Text>
);
}
}
And this class to render the blinking text:
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = { text: 'x' };
}
render() {
return (
<View style={styles.container}>
<Text style={styles.other}>$ root | > </Text>
<BlinkerFluid text=' _' />
<TextInput style={styles.lol}
onChangeText={(text) => this.setState({text})}
value={this.state.text}
/>
</View>
);
}
}
I want to be able to stop the blinking of the text when I click on the TextInput prop and/or write to it.
How would I go about doing that?
I've been trying to search for an answer on my own, read the docs, but nothing has helped.
Any help is appreciated!

You could try something like the below code snippet (I've only included the code for App. You can implement the paused functionality in the BlinkerFluid however you want.
Basically, I'm storing in the state whether the blinker should be paused or not, then passing that value through to the BlinkerFluid component as a prop. You can set the pauseBlinker state easily, perhaps on onChangeText as in the example, or on press of a TouchableOpacity element etc.
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
text: 'x',
pauseBlinker: false,
};
}
render() {
return (
<View style={styles.container}>
<Text style={styles.other}>$ root | > </Text>
<BlinkerFluid text=' _' paused={this.state.pauseBlinker} />
<TextInput style={styles.lol}
onChangeText={(text) => this.setState({ text, pauseBlinker: true, })}
value={this.state.text}
/>
</View>
);
}
}

Related

Error: Object are not valid as a react child?

export default class App extends Component<Props> {
constructor() {
super();
this.state = {
value: "Edit me!"
};
this.handleChangeText = this.handleChangeText.bind(this);
}
handleChangeText(newText) {
this.setState({
value: newText
});
}
render() {
return (
<View style={styles.container}>
<TextInput style={styles.editText}
onChange={this.handleChangeText}/>
<Text>hello {this.state.value}</Text>
</View>
);
}
I am new to react native. I am getting error on hello {this.state.value} line. How can i solve this. Please help me
Can you try this?
export default class App extends Component<Props> {
constructor(Props) {
super(props);
this.state = {
value: "Edit me!"
};
}
handleChangeText = (newText) => {
this.setState({
value: newText
});
}
render() {
return (
<View style={styles.container}>
<TextInput
style={styles.editText}
onChangeText={this.handleChangeText}
/>
<Text>hello {this.state.value}</Text>
</View>
);
}
}

how to get value from react-native-numeric-input

I am new to react native. I am using this package https://www.npmjs.com/package/react-native-numeric-input .
My problem is how to get the value pass to the next page.
From using this code:
class FirstActivity extends Component {
static navigationOptions = {
title: 'Main Screen'
}
constructor(props) {
super(props)
this.state = {number: ''}
}
Send_Data_Function = () => { this.props.navigation.navigate('Second', { no: this.state.number, }); }
render() {
return (
<NumericInput value={this.state.value} onChange={value => this.setState({number : value})}
<TouchableOpacity activeOpacity={.4} style={styles.TouchableOpacityStyle} onPress={this.Send_Data_Function} >
<Text style={styles.TextStyle}> NEXT </Text>
</TouchableOpacity>/>
)}
class SecondActivity extends Component{
static navigationOptions =
{
title: "Second Activity"
};
render() {
return (
<View style= {styles.MainContainer}>
<Text style={styles.textStyle}>
1 = {this.props.navigation.state.params.no}
</Text>
</View>
)}
}
const AppNavigator = createStackNavigator({
First : {screen: FirstActivity},
Second : {screen: SecondActivity}
});
export default createAppContainer(AppNavigator);
How to get the result? Anyone can help me?
Your increased and decreased value pass successfully on the next page but you are not passing right state, You has to pass number state in the second activity.
Send_Data_Function = () => {
this.props.navigation.navigate("Second", { no: this.state.number });
};
and read successfully by 1 = {this.props.navigation.state.params.no} code.
Your main problem is when you press to increase or decrease button, value in the middle of these buttons won't change right?
Here is your solution.
when you initialize your number state don't assign string value.Instead of string initialize number state with number
Your code:
constructor(props) {
super(props)
this.state = {number: ''}
}
Changes:
constructor(props) {
super(props)
this.state = {number: 0}//<-------------
}
Next change is you likely forgot to assign an initial value to NumericInput component that's why it won't show you any changes in state. Also same here you used the wrong state to assign value and show value. You have an initialized number state and use value state to assign the value.
Here is your solution.
Your code:
<NumericInput
value={this.state.value}//<----mistake---
onChange={value => this.setState({number : value})}/>
Solution:
<NumericInput
initValue={this.state.number}//<---Add---
value={this.state.number}//<----changes---
onChange={value => this.setState({number: value })}/>
First you need to set the state for the changing value of numeric input.
please try to do as following:
...
constructor(props) {
super(props);
this.state = {
numericInputValue: ''
}
}
onChangeNumericInputValue = (value) => {
this.setState({ numericInputValue: value });
}
render() {
...
<NumericInput type='up-down' onChange={this.onChangeNumericInputValue} />
...
}
...
The next step is to pass the numericInputValue to the next page.
if you're using react-navigation for navigating between scenes, you can do it as following:
this.props.navigation.navigate('SceneName', { numericInputValue: this.state.numericInputValue })
Try this in your secondActivity
-->
class SecondActivity extends Component{
static navigationOptions =
{
title: "Second Activity"
};
constructor(props){
super(props);
this.state={
numericInputValue:props.navigation.getParam("no",null)
}
}
render() {
return (
<View style= {styles.MainContainer}>
<Text style={styles.textStyle}>
1 = {this.state.numericInputValue}
</Text>
</View>
)}
}

onChangeText setState only sets one character not the whole string

I accept text input from a search bar and setState to text but its only one character at a time how can I continue to update and add to the state rather than replace what's in state? I feel like this is the intended action but my code does not do this.
class FindRecipesScreen extends Component {
static navigationOptions = {
title: "Find Recipes",
header: null
};
constructor(props) {
super(props);
this.state = {
search: "",
recipe: "",
text: "",
};
}
backToHomePage = () => {
this.props.navigation.navigate("Home");
};
componentDidMount() {
this.props.getRecipeList(this.props.auth.jwt);
}
handleSearch = text => {
console.log("text", text);
this.setState({text: text});
};
render() {
return (
<View style={styles.recipe}>
<View style={styles.recipeBar}>
<ActionNavbar title="Find Recipes"
leftAction={this.backToHomePage}
leftIcon={require("app/assets/icons/cancel.png")}
rightAction={this.backToHomePage}
rightIcon={require("app/assets/icons/filter.png")}/>
</View>
<View>
<View>
<SearchBar
containerStyle={styles.searchContainer}
inputContainerStyle={styles.searchInputContainer}
inputStyle={styles.searchInput}
lightTheme
searchIcon={searchIcon}
round
onChangeText={this.handleSearch}
placeholder="Search Cookbooks"
/>
<View style={styles.forward}>
<Image
style={styles.forwardIcon}
width={18}
height={18}
source={require("app/assets/icons/forward.png")}
/>
</View>
</View>
</View>
</View>
);
}
}
I found the solution... I needed const { search } = this.state; after render but before return with searcher
Use debouncer in the handleSearch function so the state is set after your debounce time.

How change a state variable via props?

I'm trying do show a modal screen with a button, when i press the button the modal will appears. The variable that is responsable for it is modalVisible and its a variable state. But if my button is not in the same class of my screen Profile i'm not able to change the variable, at last i dont know a way to do:
class Carrousel extends React.Component {
render () {
return (
<Button
title= 'press me to show up'
onPress= {() => this.props.visible = true}
/>
);
}
}
export default class Profile extends React.Component {
constructor(props) {
super(props);
this.state = {
modalVisible: true,
};
}
render() {
return (
<View>
<Modal
visible = {this.state.modalVisible}>
</Modal>
<Carrousel visible = {this.state.modalVisible}/>
</View>
);
}
}
Is there an way to change this variable modalVisible in another class? Or is there another way to i show the modal with a button in another class?
*The carrousel class is another file Carrousel.js.
You have to set a function as a prop that changes the state on the Parent Component. I haven't tried but it should be something like this:
class Carrousel extends React.Component {
render () {
return (
<Button
title= 'press me to show up'
onPress= {() => this.props.setVisible()}
/>
);
}
}
export default class Profile extends React.Component {
constructor(props) {
super(props);
this.state = {
modalVisible: false,
};
this.setVisible = this.setVisible.bind(this);
}
setVisible() {
this.setState({
modalVisible: true,
})
}
render() {
return (
<View>
<Modal
visible = {this.state.modalVisible}>
</Modal>
<Carrousel setVisible={this.setVisible}/>
</View>
);
}
}

Passing React Native Input Field to State

Background
I am working with a React Native application and I have a login form. The login form input field is passing email to state on button click.
I am trying to console.log the email field. this.state.email but I keep getting email not defined.
I have removed the firebase functions to make it easier to read. But at the top of the form the console.log never succeeds.
Examples
These are the components I am working with.
Input Component
This input component is to be used in LoginForm.
class Input extends Component {
constructor(props) {
super(props);
this.state = {
value: '',
onChangeText: ''
}
}
render() {
return(
<View}>
<Text>{this.props.label}</Text>
<TextInput
value={this.value}
onChangeText={this.onChangeText}
/>
</View>
);
}
}
LoginForm Component
class LoginForm extends Component {
constructor(props) {
super(props);
this.state = {
email: '',
};
}
signIn() {
console.log(this.state.email);
}
render() {
return(
<Card>
<CardSection>
<Input
value={this.state.email}
onChangeText={(email) => this.setState({email})}
/>
</CardSection>
<CardSection>
<Button
onPress={() => this.signIn()}
btnTxt='Sign In'
/>
</CardSection>
</Card>
);
}
Question
What is the proper way to pass the input field "email" to state so I can use it in my Firebase Authentication function?
In the code featured in your question, your Input component is calling this.onChangeText on onChangeText, this isn't referencing the same function you passed down as a prop from LoginForm.
class Input extends Component {
constructor(props) {
super(props);
this.state = {
value: '',
onChangeText: '',
};
}
render() {
return (
<View>
<Text>{this.props.label}</Text>
<TextInput
value={this.value}
onChangeText={email => this.props.onChangeText(email)}
/>
</View>
);
}
}