I'm new to react native development. And I have created this sample note application using Redux for react native. In this application user must be able to add a note and that note needs to be managed via redux store.
And I have created required action and reducer for adding note functionality.
However I was not able to add a new note to the store. I tried debugging and when it reaches the "ADD_Note" case in reducer, it jump into default case automatically. And whatever the string value or note that pass via action, it becomes 'undefined' in the reducer.
Please refer my code for adding note screen
import React, {useState, useEffect} from 'react';
import {
StyleSheet,
View,
Text,
TextInput,
Button,
FlatList,
} from 'react-native';
import {useSelector, useDispatch} from 'react-redux';
import * as NoteActions from '../store/actions/Note'; // import note actions
const NoteScreen = () => {
const [note, setNote] = useState('');
const dispatch = useDispatch(); // use dispatch
const noteHandler = text => {
setNote(text);
};
return (
<View style={styles.container}>
<View>
<TextInput
style={styles.textInput}
placeholder="Enter a note"
onChangeText={noteHandler}
value={note}
/>
<Button
title="Add"
onPress={() => {
console.log(note);
dispatch(NoteActions.addNote(note));
}}
/>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
width: '100%',
margin: 10,
},
textInput: {
width: '100%',
marginBottom: 10,
textAlign: 'center',
borderBottomWidth: 1,
},
});
export default NoteScreen;
And below is the action js file.
// action types
export const ADD_NOTE = 'ADD_NOTE'
//action creators
export function addNote(note){
return{
type:ADD_NOTE,
date:note
}
}
And below is the reducer js file
// initial state
const initialState ={
noteList:[]
}
// import actions
import ADD_NOTE from '../actions/Note';
function noteReducer (state=initialState, action){
debugger;
switch(action.type){
case ADD_NOTE:
const newNote = action.data
return{
...state,
noteList: state.noteList, newNote
}
default:
return state;
}
}
export default noteReducer;
Please help me.
Thanks in advance,
Yohan
You need to add layer of dispatch at your action, also watch the typo date instead data
export const addNote = (note) => (dispatch, getState) => {
return dispatch({
type:ADD_NOTE,
data :note
})
}
Related
I want to fetch data only which data I call in my app, it's working very well but why I am getting all those unnecessary parts, how to remove that, thanks for your support. please check the database image and display output image how it's displaying. I don't want those parts where I marked red lines, because I have not call them in my code. Is it FlatList issue or Firebase issue?
import React, { useState, useEffect } from 'react';
import { ActivityIndicator, FlatList, View, Text } from 'react-native';
import {firebase} from '../config';
const Testing = ({ navigation }) =>{
const [loading, setLoading] = useState(true); // Set loading to true on component mount
const [users, setUsers] = useState([]);
useEffect(() => {
const subscriber = firebase.firestore()
.collection('testing')
.onSnapshot(querySnapshot => {
const users = [];
querySnapshot.forEach(documentSnapshot => {
users.push({
...documentSnapshot.data(),
key: documentSnapshot.id,
});
});
setUsers(users);
setLoading(false);
});
// Unsubscribe from events when no longer in use
return () => subscriber();
}, []);
if (loading) {
return <ActivityIndicator />;
}
return (
<FlatList
data={users}
renderItem={({ item }) => (
<View style={{ height: 50, flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>ID: {item.One}</Text>
<Text>Name: {item.five}</Text>
</View>
)}
/>
);}
export default Testing;
It looks like you've incomplete or "optional" data in your backend. If you don't want to render empty fields you can conditionally render them.
For the users data that is missing both properties you can filter the data prop.
Example:
<FlatList
data={users.filter(({ One, five }) => One || five)}
renderItem={({ item }) => (
<View style={{ .... }}>
{item.One && <Text>ID: {item.One}</Text>}
{item.five && <Text>Name: {item.five}</Text>}
</View>
)}
/>
I am new to Expo and ReactNative. At the moment, I am developing a mobile app that utilizes Firebase Auth and Firestore for authentication and user profile. From googling, I found a solution to use ContextProvider for the Firebase Auth. I tried to adjust the code to obtain the profile at the same time.
This is the code for AuthenticatedUserProvider.js
import React, { useState, createContext } from 'react';
export const AuthenticatedUserContext = createContext({});
export const AuthenticatedUserProvider = ({ children }) => {
const [user, setUser] = useState(null);
const [profile, setProfile] = useState(null);
return (
<AuthenticatedUserContext.Provider value={{ user, setUser, profile, setProfile }}>
{children}
</AuthenticatedUserContext.Provider>
);
};
And I read the profile on the RotNavigator.js below
import React, { useContext, useEffect, useState } from 'react';
import { View, ActivityIndicator } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import * as eva from '#eva-design/eva';
import { ApplicationProvider, IconRegistry } from '#ui-kitten/components';
import { EvaIconsPack } from '#ui-kitten/eva-icons';
import { auth, fs } from '../config/firebase';
import { AuthenticatedUserContext } from './AuthenticatedUserProvider';
import AuthStack from './AuthStack';
import HomeStack from './HomeStack';
import { default as theme } from '../orange-theme.json';
export default function RootNavigator() {
const { user, setUser, profile, setProfile } = useContext(AuthenticatedUserContext);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
// onAuthStateChanged returns an unsubscribe
const unsubscribeAuth = auth.onAuthStateChanged(async authenticatedUser => {
try {
if (authenticatedUser) {
await setUser(authenticatedUser)
const response = await fs.collection("members").doc(authenticatedUser.uid).get();
if (response.exists) {
let data = response.data();
// console.log(data);
await setProfile(data);
// console.log(profile);
}
} else {
await setUser(null);
await setProfile(null);
}
setIsLoading(false);
} catch (error) {
console.log(error);
}
});
// unsubscribe auth listener on unmount
return unsubscribeAuth;
}, []);
if (isLoading) {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<ActivityIndicator size='large' />
</View>
);
}
return (
<>
<IconRegistry icons={EvaIconsPack} />
<ApplicationProvider {...eva} theme={{ ...eva.light, ...theme }}>
<NavigationContainer>
{user ? <HomeStack /> : <AuthStack />}
</NavigationContainer>
</ApplicationProvider >
</>
);
}
The code looks like working except that if I log in, sometimes I saw an error on profile = null and cannot read the profile but then it refreshed by itself and back to run properly.
Can someone suggest the correct way to display the profile from Firestore?
The reason I do it like this is I want an efficient way of displaying the profile since HomeStack has 3 screens that would need to display some parts of the profile on each screen.
Thank you for your help.
I added redux to create-react-app and i've been trying to get a navigator to work. I do this by having the active "page link" highlighted. The code I use for this is a combination of react hooks (using state to remember current page) and the npm package classNames.
classNames(object['key'] && classes.activeItem)
So here I have object['key'] evaluate to true when that particular item is activated so that the item gains the activeItem class.
When I replace object['key'] with true, it works. When I console.log object['key'] after I click it, it also evaluates to true.
Why isn't this working? Thanks!
import React, { useEffect, memo } from 'react';
import { bindActionCreators, compose } from 'redux';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { withStyles } from '#material-ui/core/styles';
import { loadPage } from './actions';
import { uploadFile } from '../uploadFile/actions';
import _ from 'lodash';
const styles = theme => ({
item: {
paddingTop: 4,
paddingBottom: 4,
color: 'rgba(255, 255, 255, 0.7)',
'&:hover': {
backgroundColor: 'rgba(255, 255, 255, 0.08)',
},
},
itemPrimary: {
color: 'inherit',
fontSize: theme.typography.fontSize,
'&$textDense': {
fontSize: theme.typography.fontSize,
},
},
itemActiveItem: {
color: '#4fc3f7',
},
textDense: {}
});
function Navigator(props) {
const { classes, curPage, onUploadFile, onPageChange, dispatch, ...other } = props;
let activePage = {
'invite': false,
}
useEffect(() => {
if(!curPage){
onPageChange('search');
}
activePage = _.mapValues(activePage, () => false);
activePage[curPage] = true
});
return (
<Drawer variant="permanent" {...other}>
<List disablePadding>
<ListItem button className={classNames(classes.logo)}>
<img src={require("assets/img/logo.png")} alt={''}/>
</ListItem>
<ListItem className={classes.categoryHeader} >
<ListItemText classes={{ primary: classes.categoryHeaderPrimary }}>
Files
</ListItemText>
</ListItem>
<ListItem
button
dense
className={classNames(classes.item, activePage['invite'] && classes.itemActiveItem)}
onClick={() => {onPageChange('invite')}}
>
<ListItemIcon><PeopleIcon /></ListItemIcon>
<ListItemText classes={{ primary: classes.itemPrimary, textDense: classes.textDense }}>
Invite To Your Team
</ListItemText>
</ListItem>
</List>
</Drawer>
);
}
Navigator.propTypes = {
classes: PropTypes.object.isRequired,
onPageChange: PropTypes.func.isRequired,
onUploadFile: PropTypes.func.isRequired
};
const mapStateToProps = (state) => {
const { curPage } = state.app;
return { curPage };
};
const mapDispatchToProps = (dispatch) => {
return {
onPageChange: bindActionCreators(loadPage, dispatch),
onUploadFile: bindActionCreators(uploadFile, dispatch),
dispatch
};
};
const withConnect = connect(
mapStateToProps,
mapDispatchToProps
);
export default compose(withConnect, memo, withStyles(styles))(Navigator);
Note that function passed to the useEffect hook is always run after the render.
Your useEffect doesn't cause a re-render for component to see the changes. Only change to state causes a re-render. If you want a re-render, you need to use useState hook first, and then you need to setState from within the useEffect hook. Or, you could just run these two lines as part of a render (removing them from the useEffect hook, putting them outside):
activePage = _.mapValues(activePage, () => false);
activePage[curPage] = true
useEffect(() => {
if(!curPage){
onPageChange('search');
}
});
But as I'm looking at your code, I think you could just use curPage === 'invite' && classes.itemActiveItem instead of activePage['invite'] && classes.itemActiveItem and remove those unnecessary lines related to activePage object. It would make things much easier.
I am trying to get redux working in my react-native app. Basically, I have a signIn action defined in my authActions.js file:
const signInAction = () => {
return {
type: 'signIn',
};
};
export { signInAction };
Then I have an authReducer defined as this in authReducer.js:
const initialState = {
isAuthenticated: false,
}
const authReducer = (state = initialState, action) => {
switch(action.type) {
case "signIn":
return Object.assign({}, state, {
isAuthenticated: true,
})
default: return state;
}
};
export default authReducer;
I combine that reducer in my rootReducer.js file
import { combineReducers } from 'redux';
import auth from 'app/src/redux/reducers/authReducer.js';
const rootReducer = combineReducers({
auth,
});
export default rootReducer;
and then created a store in reduxIndex.js:
import { createStore, applyMiddleware } from 'redux';
import thunkMiddleware from 'redux-thunk';
import rootReducer from 'app/src/redux/reducers/rootReducer.js';
let store = createStore(rootReducer, applyMiddleware(thunkMiddleware));
export default store;
I wrapped my app in a <Provider> component, and that seems to be working fine (I can read from the state and see the value of isAuthenticated. However, when I try to dispatch an action using mapDispatchToProps in one of my views the function is undefined:
// More imports
// ...
import { connect } from 'react-redux';
import { signInAction } from 'app/src/redux/actions/authActions.js';
const mapStateToProps = (state) => {
return {};
}
const mapDispatchToProps = (dispatch) => {
return {
onSignIn: () => { dispatch(signInAction) },
};
}
class SignIn extends Component {
constructor(props) {
super(props);
this.state = {
email: "",
password: "",
}
}
onSignInPress() {
// ******* this is where the error occurrs ****
this.props.onSignIn();
}
render() {
const {navigation} = this.props;
return (
<View style={SignInStyles.container}>
<ScrollView>
<View>
<Button
large
title="SIGN IN"
backgroundColor={colors.primary}
onPress={this.onSignInPress}
/>
</View>
</ScrollView>
</View>
);
}
export default connect(mapStateToProps, mapDispatchToProps)(SignIn);
I cant really see where I am going wrong, but im sure its a simple mistake somewhere. The specific error I get is :
"undefined is not an object. Evaluating this.props.onSignIn"
The onSignInPress callback isn't bound to any particular object, so when it gets called this is undefined.
The easy way to fix it is to use arrow syntax to make it always be bound. In your class definition:
onSignInPress = () => {
this.props.onSignIn();
}
Google found me this Medium article from Miron Machnicki which explains the differences and possible alternative syntaxes in pretty good detail.
Goal
To click the next button and dispatch two actions to the redux store that:
Firstly, update the skipAmount value.
And then use the updated skipAmount value to generate apiQuery (a string that is being used to make a request to a server).
Problem
The skipAmount value is not being updated between action 1 & 2
Example
I have created a CodeSandbox that clear demonstrates the issue that I am having. Notice that the skipAmount value is 100 (or one click event) ahead of apiQuery.
https://codesandbox.io/s/o2vvpwqo9
Code
Index.js
import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import { createStore } from "redux";
import App from "./App";
import reducer from "./reducer";
const store = createStore(reducer);
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
);
App.js
import React from 'react';
import { connect } from 'react-redux';
const mapStateToProps = state => ({
...state,
});
const queryGenerator = props => `www.apiExample.com?skipAmount=${props.skipAmount}`;
const ConnectedApp = props => (
<div className="App">
<button
onClick={() => {
props.dispatch({ type: 'SET_SKIP_AMOUNT_PLUS_100' });
props.dispatch({ type: 'SET_API_QUERY', payload: queryGenerator(props) });
}
}
>
Next
</button>
<p>Skip amount on redux: {props.skipAmount}</p>
<p>Query being generated: {props.apiQuery}</p>
</div>
);
export default connect(mapStateToProps)(ConnectedApp);
reducer.js
const reducerDefaultState = {
skipAmount: 0,
apiQuery: 'www.apiExample.com',
};
export default (state = reducerDefaultState, action) => {
switch (action.type) {
case 'SET_SKIP_AMOUNT_PLUS_100':
return {
...state,
skipAmount: state.skipAmount + 100,
};
case 'SET_API_QUERY':
return {
...state,
apiQuery: action.payload,
};
default:
return state;
}
};
In App.js queryGenerator(props) you are passing the unchanged props from the onClick.
props are'nt changing from SET_SKIP_AMOUNT_PLUS_100 until rerender.
onClick={() => {
props.dispatch({ type: 'SET_SKIP_AMOUNT_PLUS_100' });
props.dispatch({ type: 'SET_API_QUERY', payload: queryGenerator(props) });
}
In 'SET_SKIP_AMOUNT_PLUS_100' you are changing the redux state. (not the current props in component),
and in 'SET_API_QUERY' your are using the components props (not what's in redux) because props has'nt updated yet.