1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| class FlowerDetail extends Component { constructor(props) { super(props);
this.state = { title: undefined, image: undefined, description: undefined, } }
getFlowerDetail = flowerID => { request({ url: `${address.flowers}${flowerID}/`, hasCert: false, }) .then(resp => { const { title, image, description } = resp
this.setState({ title, image, description, }) }) .catch(err => { console.log('detail err: %o', err) }) }
componentWillMount() { const { flowerID } = this.props.match.params const { flowersResult, flowersEntities } = this.props
if (flowersResult.length) { console.log('use redux data') const { title, image, description } = flowersEntities[flowerID]
this.setState({ title, image, description, }) } else { console.log('use get data') this.getFlowerDetail(flowerID) } }
render() { const { title, image, description } = this.state return ( <div style={{ textAlign: 'center' }}> <img style={{ width: '70%', height: 400, marginTop: 30 }} src={image} alt={title} /> <h3>名字:{title}</h3> <p>描述:{description}</p> </div> ); } }
const mapStateToProps = state => { return { flowersResult: state.result.flowers, flowersEntities: state.entities.flowers, } }
export default connect( mapStateToProps, )(FlowerDetail)
|