2

I'm actually working on a small react app, I actually want to connect my component to firebase, but this component contains multiple classes and multiple exports, so when i apply my method (which is based on one class component) it rendering me nothing, it supposed to returns data from firestore.

when i try to console log the state on mapStateToProps it returns undefined :

const mapStateToProps = (state) => {
   console.log("state firebase",state);
   return {
     animationsfb: state.firestore.ordered.animations,
   }
}

that's my component that contains multiple classes:

export class AnimationScreen extends Component {

  render() {
    return (
         <View>
          .........
         </View>
    );
  }
}

const mapStateToProps = (state) => {
    console.log("state firebase",state);
 return {
   animationsfb: state.firestore.ordered.animations,
 }
}

class DetailsScreen extends React.Component {

    render() {

        return (
           <View>
             .........
           </View>
        );
     }
}

const Navigator = FluidNavigator({

        home: {screen: AnimationScreen},
        homeDetails: {screen: DetailsScreen},
},
);

class HomeTransitions extends React.Component {
    static router = Navigator.router;


    render() {
        const {navigation} = this.props;

        return (
            <Navigator navigation={navigation}/>
        );
    }
}
// it was like this before i change it: **export default HomeTransitions**

export default compose(
    connect(mapStateToProps), firestoreConnect([{ collection: 'animations'}])
    ) (HomeTransitions);

I expect to return me data on state when i console log it, but it returns undefined.

1
  • 3
    Why not just separate out the classes to their own files and export them individually? Commented Jul 29, 2019 at 16:36

1 Answer 1

1

Currently you are trying to connect everything to the store, including the navigator, which is probably not what you want to do.

If you are just using animationsfb in AnimationScreen, just connect this component to the store and use the output as a screen in your navigator:

class AnimationScreen extends Component {
  render() {
    return (
      <View>
        // [...]
      </View>
    );
  }
}

const mapStateToProps = (state) => {
  console.log("state firebase", state);
  return {
    animationsfb: state.firestore.ordered.animations,
  }
}

const AnimationScreenConnected = connect(mapStateToProps)(AnimationScreen);

Then in your navigator:

const Navigator = FluidNavigator({
  home: { screen: AnimationScreenConnected },
  homeDetails: { screen: DetailsScreen },
});
Sign up to request clarification or add additional context in comments.

3 Comments

That's actually right, now it returns the state of mapStateToProps, but the value animationsfb returns undefined !
this.props.animationsfb should be available in AnimationScreen, but there is probably a small delay during which it has not been fetched yet from the store. You can add a condition in your render function and displays a loading indicator if the value is not defined.
thanks for replying, actually i figure out what i'm missing, i just need to connect my component to the firebase collection, so i add this : const AnimationScreenConnected = compose( connect(mapStateToProps), firestoreConnect( [ { collection: 'animations'} ] ))(AnimationScreen);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.