Gökhan Geyik
Gökhan Geyik

Reputation: 313

React Native Firebase Fetching Only One Data

How can I fetch only one data and write it to Header ?
I am using firebase and react-redux.

firebase structure i try to write "organization": inovanka: firebase structure i try to write "**organization**": inovanka

Action File Codes:

import firebase from 'firebase';
import { Actions } from 'react-native-router-flux';
import { ORGANIZATION_NAME_DATA_SUCCESS } from './types';


 export const organizationName = () => {
    const { currentUser } = firebase.auth();
      return (dispatch) => {
        firebase.database().ref(`/organizations/${currentUser.uid}`)
          .on('value', snapshot => {
            dispatch({ type: ORGANIZATION_NAME_DATA_SUCCESS, payload: snapshot.val() });
        });
    };
  }

Reducer File :

import { ORGANIZATION_NAME_DATA_SUCCESS } from '../actions/types';

const INITIAL_STATE = {

};

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case ORGANIZATION_NAME_DATA_SUCCESS:
      console.log(action); // data retrieved as array
      return action.payload
    default:
      return state;
  }
};

Component: (I would like to write it to this)

    class HomePage extends Component {

      componentWillMount() { 
      }

      render() {
        return (
          <Container>
            <Header> 
              <Text> i would like to write it here </Text>
            </Header>
            <Content>

            </Content>
          </Container>
        );
      }
    }

    const mapStateToProps = ({ homepageResponse }) => {
      const organizationArray = _.map(homepageResponse, (val, uid) => {
        return { ...val, uid }; //
      });
      return { organizationArray };
    };

    export default connect(mapStateToProps, { organizationName })(HomePage);

Upvotes: 0

Views: 438

Answers (2)

G&#246;khan Geyik
G&#246;khan Geyik

Reputation: 313

Solution is Here !

Action File:

export const organizationName = () => {
    const { currentUser } = firebase.auth();
      return (dispatch) => {
        firebase.database().ref(`/organizations/${currentUser.uid}`)
        .once('value', snapshot => {
          _.mapValues(snapshot.val(), o => {
            console.log(o);
          dispatch({ type: ORGANIZATION_NAME_DATA_SUCCESS, payload: {organization: o.organization, fullname: o.fullname }});
          });
        });
    };
  }

Reducer File

const INITIAL_STATE = {
  organization: '',
};

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case ORGANIZATION_NAME_DATA_SUCCESS:
      console.log(action);
      return {...state, organization:action.payload.organization };
    default:
      return state;
  }
};

Component File MapToStateProps and componentWillMount

 const mapStateToProps = state =>  {
    const { organization, fullname  } = state.homepageResponse;
    console.log("burada" + organization);
       return { organization, fullname };
};




 componentWillMount(){
    this.props.organizationName();
  }

*Last Step Header *

render() {
    return (
      <Container>
        <Header>
          <Text>  { this.props.organization } </Text>
        </Header>
      </Container>
}

Thank You Everyone

Upvotes: 0

Peter Haddad
Peter Haddad

Reputation: 80914

Change this:

firebase.database().ref(`/organizations/${currentUser.uid}`)
      .on('value', snapshot => {

to this:

firebase.database().ref(`/organizations/${currentUser.uid}`)
      .once('value', snapshot => {

using once() will read data only one time, thus fetching only one data

Upvotes: 3

Related Questions