2017-06-20 19 views
0

stateからの既存のデータ(redux-persistから提供される)を持つコンポーネントをレンダリングしようとしています。データはstate.login.userです(これはmapStateToPropsデータオブジェクトと呼ばれ、返すされている機能:state.login.userが、DataObjectが更新されていないと、そのせいでcomponentWillReceivePropsで呼び出されていないネイティブcomponentsetWillUpdateが反応しない

あなたは何イムが間違っているのに私を指すことができ

import React from 'react' 
import { connect } from 'react-redux' 
import { ScrollView, AppRegistry, Component, Text, Image, View, Button, Modal, TouchableOpacity } from 'react-native' 
import { GiftedForm, GiftedFormManager } from 'react-native-gifted-form' 

// Styles 
import styles from './Styles/MyProfileScreenStyles' 

class MyProfileScreen extends React.Component { 
    constructor (props, context) { 
    const dataObject = { 
     profile: { 
     last_name : undefined, 
     } 
    } 
    super(props, context) 
    this.state = { 
     form: { 
     lastName: dataObject.profile.last_name, 
     tos: false 
     } 
    } 
    } 

    handleValueChange (values) { 
    this.setState({form: values}) 
    } 
    componentWillReceiveProps (newProps) { 
    console.tron.log("componend will receive") 
    console.tron.log(newProps) 
    if (newProps.dataObject) { 
     this.setState({ 
     dataObject: newProps.dataObject 
     }) 
    } 
    } 
    render() { 
    const {lastName, tos, gender} = this.state.form 
    console.log('render', this.state.form) 
    return (
     <View style={styles.container}> 
     <GiftedForm 
      formName='signupForm' 
      openModal={(route) => { this.props.navigator.push(route) }} 
      onValueChange={this.handleValueChange.bind(this)} 
     > 
      <GiftedForm.TextInputWidget 
      name='lastName' 
      title='Last name' 
      placeholder='Last name' 
      clearButtonMode='while-editing' 
      value={lastName} 
      /> 
      <GiftedForm.HiddenWidget name='tos' value={tos}/> 
     </GiftedForm> 
     </View> 
    ) 
    } 
} 
const mapStateToProps = (state) => { 
    if(state.login.user !== null){ 
    console.tron.log("test map state to props") 
    return { 
     dataObject: state.login.user 
    } 
    } 
    return {} 
} 

export default connect(mapStateToProps)(MyProfileScreen) 
。?

答えて

2

componentWillReceivePropsは、コンポーネントがレンダリングされた後、コンパウンドの前に小道具が更新されたときにのみ呼び出されますntは再レンダリングされます。小道具がすでにそこにあるはずであるので、コンストラクタの内部に状態を設定したいと思うでしょう。

+0

どのようにpropsをstate.login.userに設定しますか? – AleXzpm

+0

あなたのコンストラクタで 'props.dataObject'を使うべきときに' dataObject'を定義します。 – Jwebbed

+0

this.state put 'dataObject:props.dataObject'にあります。 –

関連する問題