我如何从nextState更改新的不可变对象(静态getDerivedStateFromProps)

时间:2018-11-30 13:11:54

标签: reactjs getderivedstatefromprops

我在React中与HOC在一起。 包装组件-这只是带有输入的组件(受控组件) 和Wrap-是一个组件,必须验证受控组件并返回错误对象。

import React, { Component } from 'react';
    class Email extends Component {
        constructor(props){
        super(props)    
}

render(){   
        const { error, getDataFromHandelInput, getDataFromInputRepeat } = this.props;
        console.log(error)
    return(

        <section className="Email">
            <label> 
                <h3>Your email</h3>
                    <input 
                    type="text" 
                    placeholder="example@google.com" 
                    //email={user.email}
                    onChange={getDataFromHandelInput}
                    />
                    <p>{`Error ${error}`}</p>
                    </label>
                    <label> 
                        <text>Repeat email</text>
                        <input 
                        type="text"  
                        placeholder="example@google.com"
                        onChange={getDataFromInputRepeat}
                        />

                    </label>

        </section>
    )
    }

}
 Email.defaultProps = {error : false}
 export default Email;

我有这样的HOC

import React, { Component } from 'react';
import Email from '../components/Email';
export const EmailHOC = (Email) =>{
    return class extends Component{
    constructor(props){

     super(props)
     this.getDataFromHandelInput= this.getDataFromHandelInput.bind(this);
     this.getDataFromInputRepeat = this.getDataFromInputRepeat.bind(this);

 }

  state={
      error: [],
      email : "",
      repeatEmail: ""
  }

    getDataFromHandelInput = (event, value) => { 
        const inputChangeValue = event.target.value.substr(0,20);
        if(inputChangeValue !== this.state.email){
            this.setState({
                email : inputChangeValue
            })
        }
    }

    getDataFromInputRepeat = (event, value) => {
        const inputRepeatChange = event.target.value.substr(0,20)
        if(inputRepeatChange !== this.state.repeatEmail){
            this.setState({
                repeatEmail : inputRepeatChange
            })
        }
    }

    static getDerivedStateFromProps(props, nextState, prevState) {
        const reg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        console.log(props)
        //console.log(nextState.email);
        //console.log(nextState.repeatEmail);

        //const arrayWithErrors = prevState.error.filter(error => error !== "This field might be contain more than 6 symbols");
        //console.log(arrayWithErrors)
        const arrayWithErrors  = nextState.error;
        console.log(`NEXTSTATE ${arrayWithErrors}`)
        if((nextState.email.length < 6 )&&(prevState===[] || prevState !== "This field might be contain more than 6 symbols")){
            return nextState.error = nextState.error.concat("This field might be contain more than 6 symbols");

        }

        if(reg.test(nextState.email) === false){
            nextState.error = nextState.error.concat("Please, check your email");
        };
        if(reg.test(nextState.repeatEmail) === false){
            nextState.error = nextState.error.concat("Please, repeat right email");
        }
        return nextState.error
}

    render(){  

        const { user } = this.props;
        const { error } = this.state;
        //console.log("ERROR", error)
        return(

            <Email
            {...this.props}
            getDataFromHandelInput={this.getDataFromHandelInput}
            getDataFromInputRepeat={this.getDataFromInputRepeat}
            error={error}
            />

        )
    }
    }
 }
const EmailValidation = EmailHOC(Email);
export default EmailValidation;

我对带有错误的对象有疑问,因为我只需要一个不同的属性(没有很多相同的属性),而且我没有opinios,该如何调试它

0 个答案:

没有答案