Is it against the philosophy of contraction in order to copy the state of the reduction to a local state?

import React, { Component } from 'react'
import _ from 'lodash'
import { PageHeader, Row, Col, FormGroup, ControlLabel, Button, FormControl, Panel } from 'react-bootstrap'

import FieldGroup from '../fieldGroup/fieldGroup'
import PassagePreview from './preview'

class PassageDetail extends Component {
  constructor(props) {
    super(props)

    this.handleChange = this.handleChange.bind(this)

    this.state = {
      passageDetails: {}
    }

  }

  componentDidMount() {
    this.props.fetchPassage(this.props.params.passageId)
  }

  componentWillReceiveProps(nextProps) {
    if(nextProps.passageState.passageStatus === 'success') {
      this.setState({passageDetails: nextProps.passageState.passage})
    }
  }

  handleChange(e) {
    let passageDetails = this.state.passageDetails

    passageDetails[e.target.name] = e.target.value

    this.setState({passageDetails})
  }

  render() {
    if(this.props.passageState.passageStatus !== 'success') {
      return null
    }
    return (
      <div>
        <PageHeader>
          Create Passage
        </PageHeader>
        <Row>
          <Col md={6}>
            <FieldGroup
              type="text"
              label="Passage Title"
              name="title"
              onChange={this.handleChange}
              value={this.state.passageDetails.title}
            />

            <FieldGroup
              type="text"
              label="Passage Author"
            />
            <FormGroup>
              <ControlLabel>Passage Text</ControlLabel>
              <FormControl componentClass="textarea" placeholder="textarea" rows="20" />
            </FormGroup>
          </Col>
          <Col md={6}>
            <PassagePreview passageDetails={this.state.passageDetails} />
          </Col>
        </Row>

        <Row>
          <Col md={6} mdOffset={6}>
            <Button block bsStyle="primary">Create Passage</Button>
          </Col>
        </Row>
      </div>
    )
  }
}

export default PassageDetail

This is my component. Container:

import { connect } from 'react-redux'
import * as PassagesActions from '../actions/passages'
import PassageMain from '../components/passage/main'

const mapStateToProps = (state) => {
  return {
    passageState: state.PassageReducer
  }
}

const mapDispatchToProps = (dispatch) => {
  return {
    fetchPassages: () => {
      const params = {
        url: `${process.env.REACT_APP_API_ENDPOINT}/passages`
      }
      dispatch(PassagesActions.fetchData(params, 'passages'))
    },
    fetchPassage: (passageId) => {
      const params = {
        url: `${process.env.REACT_APP_API_ENDPOINT}/passages/${passageId}`
      }
      dispatch(PassagesActions.fetchData(params, 'passage'))
    }
  }
}

export default connect(
  mapStateToProps, mapDispatchToProps
)(PassageMain)

So, you can see that in mine componentDidMountI componentWillReceivePropsam making an API call (via redux) and in , I am copying this to my local state. The idea is that if I make changes to mine passage, this is done at the component level, then I can pass this object back through some action that has not yet been created redux.

Is this acceptable in the philosophy of contraction? I still hug my head around the redux, so I apologize if this question makes no sense.

+4
source share
3 answers

, . Redux: http://redux.js.org/docs/faq/OrganizingState.html#organizing-state-only-redux-state:

. , - , , . , , .

, Redux:

  • ?
  • ?
  • ?
  • (.. )?
  • (.. , , )?

, " " , .

+3

" " , , , ! , , . Redux , , , , .

, , , passages Redux.

passage, , , , , , .

componentWillReceiveProps, , , . , ( , id).

Redux - , . , , :

  • , , Redux.
  • , , -
  • , ,

: , . , , , , , React , .

+2

"Redux" , . , , .

The problem is that the state of the components will not be synchronized with the repository, therefore, if the repository is updated and re-renovated, the component can receive new details (possibly if nothing else updates state.PassageReducer) and redefine the local state.

Take a look at redux docs on Unidirectional data flow and the todo example where they add new todos .

+1
source

Source: https://habr.com/ru/post/1670979/


All Articles