options dialog in React Native Paper - react-native

I want to implement the options dialog from React Native Paper:
However, the example code (link to example code) from React Native Paper is different.
There doesn't seem to be functionality in React Native Paper to create the options dialog.
The best suitable option I know of is https://github.com/react-native-picker/picker, but that's not really the UI I'm looking for.
Any advice would be much appreciated!

You can update the sample source code of react-native-paper into this:
import * as React from 'react';
import { View } from 'react-native';
import { Button, Paragraph, Dialog, Portal, Provider, RadioButton, Text} from 'react-native-paper';
const MyComponent = () => {
const [visible, setVisible] = React.useState(false);
const [value, setValue] = React.useState('Option 1');
const showDialog = () => setVisible(true);
const hideDialog = () => setVisible(false);
return (
<Provider>
<View>
<Button onPress={showDialog}>Show Dialog</Button>
<Portal>
<Dialog visible={visible} onDismiss={hideDialog}>
<Dialog.Title>Choose an option</Dialog.Title>
<Dialog.Content>
<RadioButton.Group onValueChange={newValue => setValue(newValue)} value={value}>
<View>
<Text>Option 1</Text>
<RadioButton value="option 1" />
</View>
<View>
<Text>Option 2</Text>
<RadioButton value="option 2" />
</View>
<View>
<Text>Option 3</Text>
<RadioButton value="option 3" />
</View>
</RadioButton.Group>
</Dialog.Content>
<Dialog.Actions>
<Button onPress={hideDialog}>OK</Button>
<Button onPress={hideDialog}>Cancel</Button>
</Dialog.Actions>
</Dialog>
</Portal>
</View>
</Provider>
);
};
export default MyComponent;

Related

Pass data between components in React Native

I've been reading the documentation and searching code here, but I can't find anything that helps me.
The idea is to change the email and password values when onChange is triggered so then it will be possible to retrieve that same data from App.js but I can't find any simple example of how this works using functional components.
After reading docs/inet seems like ReactNative has been changing a lot.
Here is my Form.js component:
import React, {useState} from 'react';
import {View, Text, TextInput} from 'react-native';
import styles from './styles';
const Form = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
return (
<View styles={styles.container}>
<Text style={styles.inputLabel}>Email</Text>
<TextInput
placeholder="Email"
onChangeText={email => setEmail(email)}
style={styles.input}
value={email}
/>
<Text style={styles.inputLabel}>Password</Text>
<TextInput
secureTextEntry={true}
placeholder="Password"
onChangeText={password => setPassword(password)}
style={styles.input}
value={password}
/>
</View>
);
};
export default Form;
And my App.js component
import React from 'react';
import {View, Button} from 'react-native';
import styles from './styles';
import Form from './Form';
import Greeting from './Greeting';
import Terms from './Terms';
const handleButton = data => {
console.log(data);
};
const App = () => {
return (
<View style={styles.container}>
<Greeting />
<Form />
<Terms />
<View style={{marginTop: 35}}>
<Button
onPress={() => handleButton(Form['email'])}
style={styles.confirmBtn}
title="Crear cuenta"
/>
</View>
</View>
);
};
export default App;
Since Form.js is the child component of the App.js you need to initialise the state in App.js and pass the setState function to Form.js like below
App.js
import React, {useState} from 'react';
import {View, Button} from 'react-native';
import styles from './styles';
import Form from './Form';
import Greeting from './Greeting';
import Terms from './Terms';
const App = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const handleButton = data => {
console.log(email);
};
return (
<View style={styles.container}>
<Greeting />
<Form setEmail={(e)=>setEmail(e)} setPassword={(p)=>setPassword(p)} />
<Terms />
<View style={{marginTop: 35}}>
<Button
onPress={() => handleButton()}
style={styles.confirmBtn}
title="Crear cuenta"
/>
</View>
</View>
);
};
export default App;
Form.js
import React, {useState} from 'react';
import {View, Text, TextInput} from 'react-native';
import styles from './styles';
const Form = ({setEmail,setPassword}) => {
return (
<View styles={styles.container}>
<Text style={styles.inputLabel}>Email</Text>
<TextInput
placeholder="Email"
onChangeText={email => setEmail(email)}
style={styles.input}
value={email}
/>
<Text style={styles.inputLabel}>Password</Text>
<TextInput
secureTextEntry={true}
placeholder="Password"
onChangeText={password => setPassword(password)}
style={styles.input}
value={password}
/>
</View>
);
};
export default Form;

RESOLVED React navigation not working react native

I am just giving a console.log (data) and navigating to another screen.
but I'm having the following error:
Component Exception
undefined is not an object
what am I doing wrong?
NOTE: When I use, navigation.goBack() works.
code below;
the error persists
Can anyone help me??
CODE UPDATED
import React, { useRef } from 'react';
import { useNavigation } from '#react-navigation/native';
import { Form } from '#unform/mobile';
import {
KeyboardAvoidingView,
Platform,
ScrollView,
Image,
} from 'react-native';
import logo from '../../assets/logo.png';
import Input from '../../components/Input';
import Button from '../../components/Button';
import { Container, FormContainer, Title, TitleContainer } from './styles';
export default function SignUp() {
const formRef = useRef(null);
const navigation = useNavigation();
const handleSignUp = data => {
console.log(data);
navigation.navigate('SignUpPersonalData', { data });
};
return (
<>
<KeyboardAvoidingView
style={{ flex: 1 }}
behavior={Platform.OS === 'ios' && 'padding'}
enabled
>
<ScrollView keyboardShouldPersistTaps="handled">
<Container>
<Image source={logo} />
<TitleContainer>
<Title>Crie sua conta</Title>
</TitleContainer>
<Form ref={formRef} onSubmit={handleSignUp}>
<Input
autoCorrect={false}
autoCapitalize="none"
keyboardType="email-address"
name="email"
icon="mail"
placeholder="E-mail"
/>
<Input
secureTextEntry
returnKeyType="send"
name="password"
icon="lock"
placeholder="Senha"
/>
<Button onPress={() => formRef.current.submitForm()}>
Continuar
</Button>
</Form>
</Container>
</ScrollView>
</KeyboardAvoidingView>
</>
);
Screenshot error:
SX
don't use useCallback, try the following.
const handleSignUp = (data) =>{
console.log(data);
navigation.navigate('SignUpPersonalData',{data: data});
}

react-native-swiper doesnt support this.props.navigation.navigate

I am new to react native, I am trying to implement react-native-swiper to my project
render() {
return (
<Swiper
loop={false}
showsPagination={false}
index={1}>
<View style={this.viewStyle()}>
<ActivityScreen />
</View>
<Swiper
horizontal={true}
loop={false}
showsPagination={false}
index={1}>
<View style={this.viewStyle()}>
<YourGroups />
</View>
</Swiper>
<View style={this.viewStyle()}>
<AlertScreen />
</View>
</Swiper>
)
}
}
But when I am in component screen and click on the button
<Button title="thank you" onPress={()=>this.props.navigation.navigate("ThankScreen")} ></Button>
I get an error
undefined is not an object (evaluating 'this.props.navigation.navigate')
I ran in to the same issue and fixed it using React Navigation, 5 which allows you to access the navigation prop from any component using the useNavigation hook:
import React from 'react';
import { Button } from 'react-native';
import { useNavigation } from '#react-navigation/native';
const GoToButton = ({ screenName }) => {
const navigation = useNavigation();
return (
<Button
title={`Go to ${screenName}`}
onPress={() => navigation.navigate(screenName)}
/>
);
}

React Native Buttons onPress nothing happens, also onChangeText not working

I am new to React Native. I have a single login form with two fields. I want to authenticate the user before sending him to Dashboard.js. No matter what I try the button is not doing anything(absolutely nothing happens,no error). I have kept the code in Login.js to show all the things I have tried to do namely
routing to dashboard.js
trying to do something with invoking the handleSubmitPress function on button press.
pop an alert.
I have used two buttons. One from react-native-paper and one from react-native.
I am giving the code of my app.js and login.js below. Please can someone help?
CODE
App.js
import React from "react";
import { NavigationContainer } from "#react-navigation/native";
import { createStackNavigator } from "#react-navigation/stack";
import DashBoard from "./mainmenu/DashBoard";
import Login from "./mainmenu/Login";
const Stack = createStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Login" component={Login} />
<Stack.Screen name="DashBoard" component={DashBoard} />
</Stack.Navigator>
</NavigationContainer>
);
}
Login.js
import React, { useState } from "react";
import { Title, TextInput} from "react-native-paper";
import { View, Alert,Button } from "react-native";
export default function Login() {
const [mobile, setMobile] = useState("123456789");
const [password, setPassword] = useState("123");
function handleSubmitPress() {
console.log({ mobile, password });
}
return (
<View
style={{
backgroundColor: "skyblue",
alignItems: "center",
justifyContent: "center",
}}
>
<TextInput
label="Your mobile number "
value={mobile}
onChangeText={(text) => setMobile(text)}
/>
<TextInput
label="Type Password "
value={password}
onChangeText={(text) => setPassword(text)}
/>
<Button
icon="camera"
type="submit"
mode="contained"
//onPress={() => props.navigation.navigate("DashBoard")}
onPress={()=>Alert.alert('Navigate pressed')}
>
Navigate
</Button>
<Button
title="Print"
onPress={() => Alert.alert('Simple Button pressed')}
// onPress={handleSubmitPress()}
/>
</View>
);
}
Change your Login component to this.
...
const Login = ({navigation}) => {
const [mobile, setMobile] = useState('123456789');
const [password, setPassword] = useState('123');
function handleSubmitPress() {
console.log({mobile, password});
}
return (
<View
style={{
backgroundColor: 'skyblue',
alignItems: 'center',
justifyContent: 'center',
}}>
<TextInput
label="Your mobile number "
value={mobile}
onChangeText={text => setMobile(text)}
/>
<TextInput
label="Type Password "
value={password}
onChangeText={text => setPassword(text)}
/>
<Button
title="title"
icon="camera"
type="submit"
mode="contained"
onPress={() => navigation.navigate('DashBoard')}>
Navigate
</Button>
<Button title="Print" onPress={() => handleSubmitPress()} />
</View>
);
};
export default App;
There were a couple problems.
I've used the default React Native buttons. So I added a title prop to one of the buttons, because that's required. I gave it the value "title", but change this to what you want or use the React Native Paper buttons.
The main problem was how you called handleSubmitPress in your button onPress.
Your onChangeText was fine, you just couldn't see the result, because the handleSubmitPress wasn't being called.
I've also used destructuring so you're able to access the navigation prop directly. You can also do const Long = (props) => {...} and use props.navigation, but either way you need to pass in something otherwise navigation will not work.

How do i make so the TextInput clears after submit in react native

I have a TextInput in my code that doesn't clear itself after submit and i have no idea on why it does that or how to solve it. I've looked at other posts that has this kinda issue? but none works or i don't really know where to place the code to make it clear itself after submiting.
Code
import React, { useState } from 'react';
import {
StyleSheet,
Text,
View,
TextInput,
Button,
} from 'react-native';
export default function AddList({ submitHandler }) {
const [text, setText] = useState('');
const changeHandler = (val) => {
setText(val);
}
return(
<View style={styles.container}>
<View style={styles.wrapper}>
<TextInput
style={styles.input}
placeholder='text'
onChangeText={changeHandler}
/>
<Button onPress={() => submitHandler(text)} title='ADD' color='#333' />
</View>
</View>
);
}
Simply create a new function after useState as below:
const onSubmit = useCallback(() => {
if (submitHandler) submitHandler(text)
setText("")
}, [text])
and modify textinput and button as below:
<TextInput
style={styles.input}
placeholder='What Tododay?'
onChangeText={changeHandler}
value={text}
/>
<Button
onPress={onSubmit}
title='ADD TO LIST'
color='#333'
/>
Do not forget to import useCallback from react.
I hope it help you.