1

i'm new at React js, i want to ask, i have a object, i want to update some property from the object, but when i change one property from the object, another property is update too and the value is undefined i try to using spread operator but when i running, it show error not defined. This is my code

export default class Message extends React.Component {
  constructor(props){
    super(props);
      this.state = {
        isLoggedIn: SystemStore.isLoggedIn(),
        profile: ProfileStore.getProfile(),
        messageInfo: {
          fullName: SystemStore.systemUser().fullName,
          site: '',
          email: '',
          phone: '',
        },
        subject: '',
        description: '',
        type: '',
        errorMessage: '',
        errorDialog: '',
        isSubmited: false,
        successMessage: '',
        submitting: false
      };
....
  clearForm(){
    this.setState({
      messageInfo: {
        ...messageInfo, //in here i get error messageInfo is not defined
          subject: '',
          description: ''//if doing this i get error SyntaxError
      }})
  }


  handleProfileChange(){
    this.setState({
      messageInfo: {
        ....messageInfo, site: ProfileStore.getProfile().primarySite.name,
      }});
    console.log(this.state.messageInfo.fullName);
    console.log(this.state.messageInfo.site);
  }

  handleSubjectChange(evt){
    this.setState({ subject: evt.target.value }, () => {
      if(this.state.messageInfo.subject === 'PRAYER') {
        this.setState({ type: 'REQUEST' });
      } else if(this.state.messageInfo.subject === 'ADDRESS') {
        this.setState({ type: 'INFORMATION' });
      } else if(this.state.messageInfo.subject === 'VISIT'){
        this.setState({ type: 'REQUEST' });
      }
    });
  }
    
  handleMessageChange(evt){
    this.setState(prevState=>({
      messageInfo: {
        ...prevState.messageInfo, description: evt.target.value
      }}));
  }

Any suggestion where i got wrong? Thank you

3 Answers3

2

You cannot directly access messageInfo. There are two ways to fetch it:

clearForm(){
    this.setState({
      messageInfo: {
        ...this.state.messageInfo,
          subject: '',
          description: ''
      }
    })
  }

or

clearForm(){
    this.setState((prevState) => {
      messageInfo: {
        ...prevState.messageInfo,
        subject: '',
        description: ''
      }
   })
  }
Darshna Rekha
  • 1,069
  • 7
  • 20
  • i had tried it but its still show SyntaxError Unexpected Token at ...this.state.messageInfo or ...prevState.messageInfo – Marvin Paulus Sep 10 '20 at 05:28
2

Your state has multiple properties. If you start out with properties messageInfo and type, for example, and then you do this.setState({ messageInfo: newMessageInfo }), then the new state will have only the messageInfo property; the type property will have been lost, because its value wasn't passed to setState.

Instead, spread the previous state into the calls of setState before defining the new property:

clearForm(){
  this.setState({
    ...this.state,
    messageInfo: {
      ...this.state.messageInfo,
      // ...

and

handleSubjectChange(evt){
  this.setState({ ...this.state, subject: evt.target.value }, () => {

and so on.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

In clearForm(){} function you cannot use messageInfo directly, you should define is a state type not variable.

        clearForm(){
         this.setState({
          messageInfo: {
           ...this.state.messageInfo, 
              subject: '',
              description: ''
           }})
        }
Ashish Mishra
  • 412
  • 4
  • 5