react-native-boilerplate icon indicating copy to clipboard operation
react-native-boilerplate copied to clipboard

Presentational and Container Components

Open alejandropaciotti opened this issue 8 years ago โ€ข 8 comments

This is not an issue about a problem.

For my point of view it is simply a bad implementation of a concept, that is Presentational and Container Components.

If you read this example provided in this post by Dan Abramov You will see clearly how the presentational is separated from the container.

Having said that, I suggest that the example container be changed as follows:

import React from 'react'
import { Platform } from 'react-native'
import { connect } from 'react-redux'
import { PropTypes } from 'prop-types'
import ExampleActions from 'App/Stores/Example/Actions'
import { isHot } from 'App/Stores/Example/Selectors'
import Example from 'App/Screens/Example'

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\nCmd+D or shake for dev menu.',
  android: 'Double tap R on your keyboard to reload,\nShake or press menu button for dev menu.',
})

class ExampleScreen extends React.Component {
  componentDidMount() {
    this.props.fetchTemperature()
  }

  render() {
    let temperature = this.props.temperatureIsLoading ? '...' : this.props.temperature
    if (temperature === null) {
      temperature = '??'
    }

    return (
      <Example
        instructions={instructions}
        temperature={temperature}
        isHot={isHot}
        temperatureErrorMessage={this.props.temperatureErrorMessage}
        onPress={this.props.fetchTemperature}
      />
    )
  }
}

ExampleScreen.propsTypes = {
  temperature: PropTypes.number,
  temperatureErrorMessage: PropTypes.string,
}

const mapStateToProps = (state) => ({
  temperature: state.example.get('temperature'),
  temperatureErrorMessage: state.example.get('temperatureErrorMessage'),
  temperatureIsLoading: state.example.get('temperatureIsLoading'),
  isHot: isHot(state),
})

const mapDispatchToProps = (dispatch) => ({
  fetchTemperature: () => dispatch(ExampleActions.fetchTemperature()),
})

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(ExampleScreen)

And next we can create a screen folder for screens and inside it I suggest create a screen for Example like this:

import React from 'react'
import { Text, View, Button } from 'react-native'
import { PropTypes } from 'prop-types'

import Style from './styles'

const ExampleScreen = ({
  instructions,
  temperature,
  isHot,
  temperatureErrorMessage,
  fetchTemperature,
}) => (
  <View style={Style.container}>
    <Text style={Style.title}>TheCodingMachine boilerplate</Text>
    <Text style={Style.text}>To get started, edit App.js</Text>
    <Text style={Style.text}>{instructions}</Text>
    <Text style={Style.text}>The weather temperature is: {temperature}</Text>
    <Text style={Style.text}>{isHot ? "It's pretty hot!" : ''}</Text>
    <Text style={Style.text}>{temperatureErrorMessage}</Text>
    <Button onPress={fetchTemperature} title="Refresh" />
  </View>
)

ExampleScreen.propsTypes = {
  instructions: PropTypes.string,
  temperature: PropTypes.number,
  isHot: PropTypes.boolean,
  temperatureErrorMessage: PropTypes.string,
  fetchTemperature: PropTypes.func,
}

ExampleScreen.defaultProps = {
  fetchTemperature: () => {},
}

export default ExampleScreen

alejandropaciotti avatar Oct 10 '18 12:10 alejandropaciotti

btw, I've the code ready for a PR.

alejandropaciotti avatar Oct 10 '18 13:10 alejandropaciotti

Thank you very much for your hard work. I will analyze your PR and give you a feedback soon.

AurelGit avatar Oct 11 '18 07:10 AurelGit

Hi @alejandropaciotti ! After analyzing your issue and your MR, we agree with you on the fact that we need to add a Screen folder in order to separate containers and presentational components. But for the refactoring of Saga and Store folders, we do not agree, we want it outside of Container folder because multiple containers can call only one saga, so why putting it in a specific container? Whatโ€™s your opinion about that? ๐Ÿ˜ƒ

JeremyDolle avatar Oct 23 '18 09:10 JeremyDolle

I think that the "sagas" and the "store" shouldn't be used outside the containers folder, if any container needed to use the functionality contained in a saga should dispatch an action. And if part of the store is needed it should be accessed using a selector.

I think that this separation would improve the scalability and isolation of each container, as well as the testibility (which I am preparing in another PR).

In summary, the containers communicate with the sagas through actions and data as parameters. And just that. They do not access the functions of the sagas directly.

And a container can be connected to any part of the store regardless of where it is located. But given a large application, for my point of view, I prefer to have separate parts of the store on each container in order to have better organization, I do not think it improves anything of the code. It is simply organization.

Thank you for taking the time to review my proposal!.

alejandropaciotti avatar Oct 23 '18 12:10 alejandropaciotti

In summary, the containers communicate with the sagas through actions and data as parameters. And just that. They do not access the functions of the sagas directly.

100% agree and that what we do

I think that the "sagas" and the "store" shouldn't be used outside the containers folder, if any container needed to use the functionality contained in a saga should dispatch an action. And if part of the store is needed it should be accessed using a selector.

What if a container calls an action of another container? Youโ€™ll need to duplicate your action, right? So, if you have 5 screens which have to display the temperature (for example), youโ€™ll do 5 actions (one for each screen) which call the same saga?

JeremyDolle avatar Oct 24 '18 07:10 JeremyDolle

Yes, this is the price of having everything modularized. It is the price of a container being contained in a folder.

But, in the hypothetical case that we have 5 screens that need to call an action, I think those containers should be contained in a parent container. And pass the function from parent to child, or maybe we should think a HOC to share that functionality.

alejandropaciotti avatar Oct 24 '18 14:10 alejandropaciotti

Weโ€™ve thought about that and for a first version, we want to keep saga away from containers. But weโ€™re really happy about your PR and we want to keep the idea of Screen folder. Thank you for your really interesting contribution ! Hope it will continue ๐Ÿ˜‰

JeremyDolle avatar Oct 31 '18 08:10 JeremyDolle

Of course I'll continue collaborating! And I understand your point of view, my change has pro, but also has cons.

We could close this issue and keep the interesting conversation that opened up, which certainly contributed more to me than to the repo.

alejandropaciotti avatar Oct 31 '18 10:10 alejandropaciotti