상세 컨텐츠

본문 제목

Blog App5 StreamEdit

React

by nata_developer 2020. 8. 20. 16:06

본문

0.앱 구조

blog-app

  • api

    • db.json

  • client

    • public

      • index.html

      • modal.html

    • src

      • actions

        • index.js

        • types.js

      • apis

        • streams.js

      • components

        • StreamCreate.js

        • StreamDelete.js

        • StreamEdit.js

        • StreamForm.js

        • StreamList.js

      • App.js

      • GoogleAuth.js

      • Header.js

      • Modal.js

    • reducers

      • authReducer.js

      • index.js

      • streamReducer.js

    • history.js

    • index.js

 

1.components/streams/streamForm.js

import React from 'react';
import { Field, reduxForm } from 'redux-form';

class StreamForm extends React.Component {
  renderError({ error, touched }) {
    if (touched && error) {
      return (
        <div className="ui error message">
          <div className="header">{error}</div>
        </div>
      );
    }
  }

  renderInput = ({ input, label, meta }) => {
    const className = `field ${meta.error && meta.touched ? 'error' : ''}`;
    return (
      <div className={className}>
        <label>{label}</label>
        <input {...input} autoComplete="off" />
        {this.renderError(meta)}
      </div>
    );
  };

  onSubmit = formValues => {
    this.props.onSubmit(formValues);
  };

  render() {
    return (
      <form
        onSubmit={this.props.handleSubmit(this.onSubmit)}
        className="ui form error"
      >
        <Field name="title" component={this.renderInput} label="Enter Title" />
        <Field
          name="description"
          component={this.renderInput}
          label="Enter Description"
        />
        <button className="ui button primary">Submit</button>
      </form>
    );
  }
}

const validate = formValues => {
  const errors = {};

  if (!formValues.title) {
    errors.title = 'You must enter a title';
  }

  if (!formValues.description) {
    errors.description = 'You must enter a description';
  }

  return errors;
};

export default reduxForm({
  form: 'streamForm',
  validate
})(StreamForm);

 

2.src/components/streams/streamEdit.js

import React from "react";
import { connect } from "react-redux";
import { fetchStream, editStream } from "../../actions";
import StreamForm from './StreamForm';
import _ from 'lodash';

class StreamEdit extends React.Component {
  componentDidMount() {
    this.props.fetchStream(this.props.match.params.id);
  }

  onSubmit = formValues => {
    this.props.editStream(this.props.match.params.id, formValues);
  };

  render() {
    if (!this.props.stream) {
      return <div>Loading</div>;
    }

      return (
      <div>
        <h3>Edit a Stream</h3>
        <StreamForm
          initialValues={_.pick(this.props.stream, 'title', 'description')}
          onSubmit={this.onSubmit}
        />
      </div>
    );
  }
}

const mapStateToProps = (state, ownProps) => {
  return { stream: state.streams[ownProps.match.params.id] };
};

export default connect(
  mapStateToProps,
  { fetchStream, editStream }
)(StreamEdit);

fetchStream 액션 호출

 

fetchStream 액션의 인자에 id값을 넣어 호출한다.

 

mapStateToProps

const mapStateToProps = (state, ownProps) => {
  return { stream: state.streams[ownProps.match.params.id] };
};

Store에서 streamReducer로 처리된 정보를 가져온다.

 

이 때 액션이 parameter로 id 값을 받기 때문에 인자로

 

ownProps.match.params.id를 주었다. 

 

export const editStream = (id, formValues) => async (dispatch) => {
  const response = await streams.patch(`streams/${id}`, formValues);

  dispatch({ type: EDIT_STREAM, payload: response.data });
  history.push("/");
};

 

ownProps

 

부모에게서 받은 Props(App.js)를 match.params로 접근하여 id를 얻는다.

 

InitialValues

        <StreamForm
          initialValues={_.pick(this.props.stream, 'title', 'description')}
          onSubmit={this.onSubmit}
        />

'React' 카테고리의 다른 글

Blog App3 StreamList  (0) 2020.08.20
React Router를 이용하여 component간에 props 넘겨주기  (0) 2020.08.20
redux-form  (0) 2020.08.20
Blog App 4 StreamCreate  (0) 2020.08.20
Blog App 2 GoogleAuth  (0) 2020.08.20

관련글 더보기