How to get the status of a React within a React Native

Asked

Viewed 603 times

0

Hello, I’m new to React Native and I’m having problems to pass the status of a Reducer to a component I made. the same is as follows:

import React from 'react'
import { connect } from 'react-redux'
import { View, Text } from 'react-native'

class Foo extends React.Component {
    render() {
        const foo = !!this.props.isEnabled ? 'sim!' : 'não!';
        return <View><Text>{foo}</Text></View>
    }
}

const mapStateToProps = state => {
    console.log(state);
    return { isEnabled: state.fooReducer.isEnabled }
}

const mapDispatchToProps = {}

export default connect(mapStateToProps, mapDispatchToProps)(Foo);

The Redux is working and I can view the status change from the console.log() that I put in the mapStateToProps I just can’t pass that state to the component and use it to manipulate the render.

What I need to do to use that state within the render?

  • 1

    And if you call the mapStateToProps within the function componentDidMount()?

  • I don’t understand. As far as I know, mapStateToProps is part of Redux... in theory there is no way I can get it inside the componentDidMount()

  • 1

    Your mapStateToProps returns the isEnabled which as you said is working, assigning the return of the same in componentDidMount() you, I believe, will be able to use it in render()

  • Could you give me an example? of how to use this?

1 answer

1

Theoretically the component part is working, you are using the combine? If you don’t need to specify which return is using after the variable Estate, ex : state."REDUCER".variavel. Try to access only the state for console log., if an unexpected value comes it may be some configuration of the Redux that did not you set wrong ex a store, the preview, the return etc..

  • I am using the combineReducer and I can give a consortium.log when I put it inside the mapStateToProps... Only if I try a console.log inside the component IilMount does not appear the console, even if the one in mapStateToProps appears

Browser other questions tagged

You are not signed in. Login or sign up in order to post.