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.
source
share