React-native useState - react-native

I'm new and need to use useState but how do I bind the button in a separate component and the Text in another component?
components 1
const Bottom = () => {
const [counter, setCounter] = useState(0);
function number() {
setCounter(counter + 1);
}
console.log(counter);
components 1
<View style={styles.seperator}>
<TouchableOpacity style={styles.button} onPress={number}>
<Text style={styles.buttonText}>Save</Text>
</TouchableOpacity>
</View>
components 2
<Text style={styles.number}>{props.counter}</Text>
My full code
import React, {useState, useEffect} from 'react';
import {View, Text, StyleSheet} from 'react-native';
import styles from './Header.styles';
const Header = props => {
return (
<View style={styles.container}>
<View style={styles.headContainer}>
<Text style={styles.title}>TODO..</Text>
<Text style={styles.number}>{props.counter}</Text>
</View>
</View>
);
};
export default Header;
import React, {useState} from 'react';
import {
View,
Text,
Stylesheet,
TouchableOpacity,
TextInput,
} from 'react-native';
import {Header} from 'react-native/Libraries/NewAppScreen';
import styles from './Bottom.styles';
const Bottom = () => {
const [counter, setCounter] = useState(0);
function number() {
setCounter(counter + 1);
}
console.log(counter);
return (
<View style={styles.bottomContainer}>
<TextInput
style={styles.input}
placeholder="Todo"
onChangeText={text => setText(text)}></TextInput>
<View style={styles.seperator}>
<TouchableOpacity style={styles.button} onPress={number}>
<Text style={styles.buttonText}>SAVE</Text>
</TouchableOpacity>
</View>
</View>
);
};
export default Bottom;
The code has 2 components, I want the number to increase when I press the button
i uploaded all my code sorry if it's a bad question i'm new
I tried to import props and pages

What i understand is that you try to achieve this
const component1 = ({state1,setState1})=>{return()}
const component2 = ({state2,setState2})=>{return()}
then in your screen
const HomeScreen = () =>{
const [yourState1,setYourState1]= useState(someValue1);
const [yourState2,setYourState2]= useState(someValue2);
return(
<View>
<Component1 state1={yourstate1} setState1={yourSetstate1} />
<Component2 state2={yourstate2} setState1={yourSetstate2} />
</View>
)
}
Component1 can be your button, Component2 can be your Text Component
This way you can manipulate your state.

Related

Content of card does not render when state changed in react-native-deck-swiper

I did a simple example with TouchableOpacity that on press changes the count state from 1 to 2 and displays it as Text,
but the new value is not rerendered.
Any help would be much appreciated
running test on codesandbox:
test
import React, {useState} from 'react';
import {Text, View, TouchableOpacity} from 'react-native';
import Swiper from 'react-native-deck-swiper';
const App = () => {
const [count,setCount] = useState(0);
return (
<Swiper
cards={['a', 'b', 'c']}
renderCard={() => {
return (
<View>
<TouchableOpacity onPress={() => {
setCount(2);
console.log("Clicked");
}} style={{ backgroundColor: 'yellow', width:60 }}>
<Text>Increase</Text>
</TouchableOpacity>
<Text>{count}</Text>
</View>);
}}/>
);
};
export default App;

React-Native while pressing customButton nothing happens but when i press return it works

While pressing RoundButton component nothing happens, but when I press press return it works,
Here is my custom Button component
Custom Button:
import React from 'react';
import { Text, View, StyleSheet, TouchableOpacity, } from 'react-native';
export const RoundedButton = ({
style = {},
textStyle = {},
size = 125,
...props
}) => {
return( <TouchableOpacity style={[styles(size).radius,style]}>
<Text style={[styles(size).text,textStyle]}>{props.title} </Text>
</TouchableOpacity>);
};
Calling from component:
import React, {useState} from 'react';
import { Text, View, StyleSheet,TouchableHighlight } from 'react-native';
import {TextInput} from "react-native-paper";
import {RoundedButton} from '../../components/RoundedButton'
export const Focus = ({addSubject}) => {
const [focusSubject, setFocusSubject] = useState(null);
const [tempItem, setTempItem] = useState(null);
return (
<View style={styles.container}>
<View style={styles.titleContainer}>
<Text>Want something?</Text>
<View styles={styles.inputContainer} >
<TextInput onSubmitEditing={({ nativeEvent }) => {
setTempItem(nativeEvent.text);
addSubject(nativeEvent.text)
}} />
<RoundedButton size={100} title="+" onPress={()=> {addSubject(tempItem)}} />
</View>
</View>
</View>
);
}
You need to call onPress on TouchableOpacity
Custom Button:
import React from 'react';
import { Text, View, StyleSheet, TouchableOpacity, } from 'react-native';
export const RoundedButton = ({
style = {},
textStyle = {},
size = 125,
...props
}) => {
return(
<TouchableOpacity onPress={props.onButtonHandler} style={[styles(size).radius,style]}>
<Text style={[styles(size).text,textStyle]}>{props.title} </Text>
</TouchableOpacity>
)};
In your component pass onButtonHandler
Calling from component:
import React, {useState} from 'react';
import { Text, View, StyleSheet,TouchableHighlight } from 'react-native';
import {TextInput} from "react-native-paper";
import {RoundedButton} from '../../components/RoundedButton'
export const Focus = ({addSubject}) => {
const [focusSubject, setFocusSubject] = useState(null);
const [tempItem, setTempItem] = useState(null);
return (
<View style={styles.container}>
<View style={styles.titleContainer}>
<Text>Want something?</Text>
<View styles={styles.inputContainer} >
<TextInput onSubmitEditing={({ nativeEvent }) => {
setTempItem(nativeEvent.text);
addSubject(nativeEvent.text)
}} />
<RoundedButton size={100} title="+" onButtonHandler={()=> {addSubject(tempItem)}} />
</View>
</View>
</View>
);
}

How do I determine the number range by pressing the button in react native?

I created a structure here. When I press minus it decreases the number one by one. When I press the plus, it increases one by one. But I had a hard time doing this here. I want the numbers to be between 1 and 15. So when I press the plus button, it does not go up from 15 and when I press the minus button it does not go down from 1.
How can I do it?
import React, {useState} from 'react';
import {View, Text, TouchableOpacity} from 'react-native';
const Main = () => {
const [counter, setCounter] = useState(1);
return (
<View>
<TouchableOpacity onPress={() => setCounter(counter - 1)}>
<Text>-</Text>
</TouchableOpacity>
<Text>{counter <= 1 ? 1 : counter}</Text>
<TouchableOpacity onPress={() => setCounter(counter + 1)}>
<Text>+</Text>
</TouchableOpacity>
</View>
);
};
export default Main;
You can do the following way.
import React, {useState} from 'react';
import {View, Text, TouchableOpacity} from 'react-native';
const Main = () => {
const [counter, setCounter] = useState(1);
const increment =()=>{
if(counter+1 <= 15){
seCounter(counter+1);
}
}
const decrement =()=>{
if(counter-1 > 1){
seCounter(counter-1);
}
}
return (
<View>
<TouchableOpacity onPress={() => decrement()}>
<Text>-</Text>
</TouchableOpacity>
<Text>{counter <= 1 ? 1 : counter}</Text>
<TouchableOpacity onPress={() => increment()}>
<Text>+</Text>
</TouchableOpacity>
</View>
);
};
export default Main;

How to open Modal from another component in react-native

I have tried to open Modal from another component in react-native, but modal is not open. so please help me if have any solution. this is my code
modal.js
import React from './node_modules/react';
import { View, Text } from 'react-native';
const ProfileModal = () => {
return (
<View>
<Text>HELLO TEXT</Text>
</View>
);
}
export default ProfileModal;
----------------------------------------
header.js
import ProfileModal from './ProfileModal';
import { Image, View, TouchableOpacity, Text } from 'react-native';
import Modal from 'react-native-modal';
const openLoginPopup = () => {
return (
<TouchableOpacity onPress={() => this.openModal}>
<Text> Login </Text>
</TouchableOpacity>
)
}
const openModal = () => {
return (
<Modal isVisible={true}>
<ProfileModal />
</Modal>
)
}
Thanks,
In that case, react-native-modal provide callback onBackdropPress, onBackButtonPress to close.
You can use it like this:
const ProfileModal = ({ open, onClose }) => {
return (
<Modal isVisible={open} onBackButtonPress={onClose} onBackdropPress={onClose}>
<View>
<Text>HELLO TEXT</Text>{' '}
</View>
</Modal>
)
}
Then pass onClose function when using it:
<ProfileModal open={open} onClose={()=> setOpen(false)} />
It's like you write the content modal in modal file. So you can do it something like:
modal.js
import React, {useState} from 'react';
import { View, Text } from 'react-native';
const ProfileModal = () => {
return (
<View>
<Text>HELLO TEXT</Text>
</View>
);
}
export default ProfileModal;
----------------------------------------
header.js
import ProfileModal from './ProfileModal';
import { Image, View, TouchableOpacity, Text } from 'react-native';
import Modal from 'react-native-modal';
const Header = () => {
const [open, setOpen] = useState(false)
return (
<View>
{/* some content */}
{/* btn trigger */}
<TouchableOpacity onPress={() => setOpen(true)}>
<Text> Login </Text>
</TouchableOpacity>
<Modal isVisible={open}>
<ProfileModal />
</Modal>
</View>
)
}
or
modal.js
import React, {useState} from 'react';
import { View, Text } from 'react-native';
const ProfileModal = ({ open }) => {
return (
<Modal isVisible={open}>
<View>
<Text>HELLO TEXT</Text>
</View>
</Modal>
);
}
export default ProfileModal;
----------------------------------------
header.js
import ProfileModal from './ProfileModal';
import { Image, View, TouchableOpacity, Text } from 'react-native';
import Modal from 'react-native-modal';
const Header = () => {
const [open, setOpen] = useState(false)
return (
<View>
{/* some content */}
{/* btn trigger */}
<TouchableOpacity onPress={() => setOpen(true)}>
<Text> Login </Text>
</TouchableOpacity>
<ProfileModal open={open} />
</View>
)
}
First of all openModal method is not part of openLoginPopup so remove this before openModal method.
Just use directly like below,
const openLoginPopup = () => {
return (
<TouchableOpacity onPress={openModal}>
<Text> Login </Text>
</TouchableOpacity>
)
}
Second issue was that you also don't need arrow function in onPress event until you want to pass some parameters in openModal method. You can directly pass the method name and it will work.

How to cross out text with onpress clickHandler

I am trying to have a button cross out text with 'line-through'. I am not sure how to connect this to the button.
'''
import React, { useState } from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
export default function App() {
const [name, setName] = useState('shaun')
const clickHandler = () => {
setName('shaun (CROSSED OUT)');
}
return (
<View style={styles.container}>
<Text>My name is {name}</Text>
<Text></Text>
<View style={styles.buttonContainer}>
<Button title='update state' onPress={clickHandler} />
</View>
</View>
);
}
You can use a state variable to keep track of it.
quick example -
import React, { useState } from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
export default function App() {
const [name, setName] = useState('shaun')
const [crossedOut, setCrossedOut] = useState(false) // extra state variable
const clickHandler = () => {
setName('shaun (CROSSED OUT)');
setCrossedOut(true) // set it to true on click
}
return (
<View style={styles.container}>
{/* apply style only if crossedOut is true */}
<Text style={crossedOut && {textDecorationLine: 'line-through'}}>My name is {name}</Text>
<View style={styles.buttonContainer}>
<Button title='update state' onPress={clickHandler} />
</View>
</View>
);
}