Z
Z
zlodiak2020-10-10 15:24:27
React
zlodiak, 2020-10-10 15:24:27

Is it possible to get rid of the container component?

I have a simple component that displays data from a store. I'm using react-redux. The problem is that instead of one component, I wrote two. I think you can get by with just one. Please tell me how to do it

Container component:

import { connect } from "react-redux";
import Messages from './Messages';

const mapStateToProps = state => {
  return {
    messages: state.messagesReducer.messages,
  }
}

export default connect(mapStateToProps, {})(Messages);


Presentation Component:
import React from 'react';
import styles from './Messages.module.css';

function Messages(props) {
  const messages = props.messages.map((item, i) => {
    return (
      <li className={styles.item} key={ i }>
        <div className={styles.date}>date: { item.date.toString() }</div>
        <div className={styles.name}>name: { item.name }</div>
        <div className={styles.gender}>gender: { item.gender }</div>
        <div className={styles.subject}>subject: { item.subject }</div>
        <div className={styles.text}>text: { item.text }</div>
      </li> 
    )
  });
  return (
    <ul className={styles.messages}>
      { messages }
    </ul>     
  );
}

export default Messages;

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question