0

Looking for an advice or best practices on this. Am I doing it right?

My Reducer:

export default function(){
    return [
      {
        id: '01',
        name: 'Cersei Lannister',
        city: 'Kings Landings'
      },
      {
        id: '02',
        name: 'Margaery Tyrell',
        city: 'Hign Garden'
      },
      {
        id: '03',
        name: 'Daenerys Targaryen',
        city: 'Dragon Stone'
      },
      {
        id: '04',
        name: 'Ygritte',
        city: 'Free Folk'
      },
      {
        id: '05',
        name: 'Arya Stark',
        city: 'Winter Fell'
      }
    ]
}

I've combined this reducer in allReducers as gotPeople. I am using this in a component myApp:

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

class MyApp extends Component {

    render(){

        const renData = this.props.gotPeople.map((data, idx) => {
            return (
                <View key={idx}>
                  <Text>{data.id}</Text>
                  <Text>{data.name} of {data.city}</Text>
                </View> 
            )
        });

        return(
            <View>
                {renData}
            </View> 
        );
    }
}

function mapStateToProps (state) {
  return {
    gotPeople: state.gotPeople
  }
}

export default connect( mapStateToProps )( MyApp )

And when I import MyApp from ./MyApp; in my index.android.js and call it in the View like <MyApp /> it works. Everything is displayed properly. I'm not sure if this is a proper way to do it? Is there any better way?

Please advice.

1 Answer 1

2

Your reducer is not actually look "reducerish", i.e. it does not reduce anything. But let's assume it's just a placeholder used for the purpose of example. In this case everything look ok, though I would rewrite your component a bit to make it shorter:

const MyApp = ({ gotPeople }) => (
  <View>
    {gotPeople.map((data, idx) => (
       <View key={idx}>
         <Text>{data.id}</Text>
         <Text>{data.name} of {data.city}</Text>
       </View> 
    )}
  </View>
);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Apart from the length of the component, there is no mistake, right? I mean I'm calling the data in the array with {renData} properly right?

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.