[英]What the proper way of importing state via props in multi step form in react
I'm trying to build an stepper form in react我正在尝试在反应中构建一个步进形式
It has a main parent Component Register
as followed:它有一个主要的父组件
Register
,如下所示:
import React, { Component } from 'react';
export class registration extends Component {
state = {
step:1,
CountryFlag: 'https://raw.githubusercontent.com/MeRahulAhire/country-calling-code-html/master/phone_icon.png',
CountryCode: ''
};
handleChange = (input) => (e) => {
this.setState({ [input]: e.target.value });
};
countryFlagHandler = () =>{
this.setState({CountryFlag : this.props.state.flagImg})
}
render() {
const { CountryFlag, CountryCode } = this.state;
const values = {CountryFlag, CountryCode };
switch (step) {
case 1:
return(
<Credential
handleChange={this.handleChange}
countryFlagHandler={this.countryFlagHandler}
values={values}
/>
)
default:
return (<h1>hello React App</h1>)
};
}
}
export default registration;
and a child component Credential
和一个子组件
Credential
import React, { Component } from 'react'
export class Credential extends Component {
state = {
flagImg: '',
};
render() {
const { values, handleChange, countryFlagHandler } = this.props;
const selectCountryChange = () => {
const img = document.querySelector('#img');
const select = document.querySelector('#country');
img.src = `https://flagpedia.net/data/flags/h80/${select.selectedOptions[0].dataset.countrycode.toLowerCase()}.webp`;
this.setState({
flagImg: `https://flagpedia.net/data/flags/h80/${select.selectedOptions[0].dataset.countrycode.toLowerCase()}.webp`
});
countryFlagHandler()
};
return (
<div>
<div class="image" onChange={selectCountryChange}>
<img src={values.CountryFlag} id="img"/>
</div>
<select id="country" onChange={handleChange('select')} defaultValue={values.select}>
<option data-countryCode="IN" value="91">India</option>
<option data-countryCode="US" value="1">US</option>
<option data-countryCode="GB" value="44">UK</option>
</select>
</div>
)
}
}
export default Credential;
what I'm trying to do is actually to sync and persist the data of <Select/>
and <img>
in Registration.js
Component as typically what we see in a stepper form.我正在尝试做的实际上是在
Registration.js
组件中同步和持久化<Select/>
和<img>
的数据,就像我们在步进表单中看到的那样。
But, As in the Credentials
,但是,就像在
Credentials
中一样,
<img src={values.CountryFlag} id="img"/>
the img.src
is actually manipulated by the function selectCountryChange
and to keep the value of img.src
persisted I thought of creating countryFlagHandler
in Registration
and importing it to Credential
img.src
实际上由 function selectCountryChange
操作并保持img.src
的值保持不变我想在Registration
中创建countryFlagHandler
并将其导入到Credential
but when i selected any value, it gave me this error:但是当我选择任何值时,它给了我这个错误:
TypeError: Cannot read property 'flagImg' of undefined TypeError:无法读取未定义的属性“flagImg”
Registration.countryFlagHandler
C:/Users/Rahul/Desktop/cfm-usersignup/src/public/form/registration.js:53
50 | this.setState({ [input]: e.target.value });
51 | };
52 | countryFlagHandler = () =>{
> 53 | this.setState({CountryFlag : this.props.state.flagImg})
| ^ 54 | }
55 |
56 |
& &
selectCountryChange
C:/Users/Rahul/Desktop/cfm-usersignup/src/public/form/credential.js:31
28 | this.setState({
29 | flagImg: `https://flagpedia.net/data/flags/h80/${select.selectedOptions[0].dataset.countrycode.toLowerCase()}.webp`
30 | });
> 31 | countryFlagHandler();
| ^ 32 | };
33 | return (
34 | <div>
Can anyone please tell me how to rectify my error?谁能告诉我如何纠正我的错误?
In the render method of the Registration component, the step
is undefined.在 Registration 组件的 render 方法中,
step
是未定义的。 You should use this.state.step
or destructure it from your state
.您应该使用
this.state.step
或从您的state
解构它。 I don't realize why you used export class
and export default
together.我不明白你为什么同时使用
export class
和export default
。 And remember, the name of a React component must start with a capital letter.请记住,React 组件的名称必须以大写字母开头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.