Javascript react本机componetWillUpdate不工作
我正在尝试使用state(由redux persist提供)中已有的数据呈现组件,其中的数据是state.login.user(我可以在正在调用的mapStateToProps函数的console.log中看到它,并返回dataObject:state.login.user,但是dataObject没有被更新,并且由于该组件,因此不会调用WillReceiveProps 你能指出我做错了什么吗Javascript react本机componetWillUpdate不工作,javascript,reactjs,mobile,react-native,Javascript,Reactjs,Mobile,React Native,我正在尝试使用state(由redux persist提供)中已有的数据呈现组件,其中的数据是state.login.user(我可以在正在调用的mapStateToProps函数的console.log中看到它,并返回dataObject:state.login.user,但是dataObject没有被更新,并且由于该组件,因此不会调用WillReceiveProps 你能指出我做错了什么吗 import React from 'react' import { connect } from '
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)
从“React”导入React
从“react redux”导入{connect}
从“react native”导入{ScrollView,AppRegistry,Component,Text,Image,View,Button,Modal,TouchableOpacity}
从“react native gifted form”导入{GiftedForm,GiftedFormManager}
//风格
从“./styles/MyProfileScreenStyles”导入样式
类MyProfileScreen扩展了React.Component{
构造函数(道具、上下文){
常量数据对象={
简介:{
姓氏:未定义,
}
}
超级(道具、背景)
此.state={
表格:{
lastName:dataObject.profile.last_name,
托斯:错
}
}
}
handleValueChange(值){
this.setState({form:values})
}
组件将接收道具(新道具){
console.tron.log(“组件将接收”)
console.tron.log(newProps)
if(newProps.dataObject){
这是我的国家({
dataObject:newProps.dataObject
})
}
}
渲染(){
const{lastName,tos,gender}=this.state.form
console.log('render',this.state.form)
返回(
{this.props.navigator.push(route)}
onValueChange={this.handleValueChange.bind(this)}
>
)
}
}
常量mapStateToProps=(状态)=>{
if(state.login.user!==null){
console.tron.log(“测试映射状态到道具”)
返回{
数据对象:state.login.user
}
}
返回{}
}
导出默认连接(MapStateTrops)(MyProfileScreen)
组件WillReceiveProps
仅在组件渲染后、组件重新渲染前更新道具时调用。您需要在构造函数内设置状态,因为道具应该已经存在。组件WillReceiveProps
仅在渲染后更新道具时调用组件在重新呈现之前已呈现。您需要在构造函数中设置状态,因为道具应该已经存在。如何将道具设置为状态。login.user?在构造函数中,您可以定义一个dataObject
,此时您应该只使用props.dataObject
。在此状态下,放置dataObject:props.dataObject
如何将道具设置为state.login.user?在构造函数中,当您应该只使用props.dataObject
时,您可以定义一个dataObject
。在这个.state中,putdataObject:props.dataObject