React: Building Dynamic Form From Previous State and Handling Error on it. Error on state
I am trying to code in the react for learning and building a dynamic form which takes values from the previous state and generates according to it.
The previous from take input count and I set to a state variable which below component uses it i.e fromObj.count
Below is the component of form :
import React,{ Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { getDesignationDepartment } from '../actions/fetchActions';
class CollegeMember extends Component{
constructor(props) {
super(props);
this.state= {
formArray : {},
errors:{},
errorClass:{}
}
}
componentDidMount(){
this.props.getDesignationDepartment();
}
handleSubmit = (e) => {
e.preventDefault();
if (this.handleValidation()) {
this.props.formData(e);
}
}
handleValidation = () => {
let count=this.props.formObj.count;
let errors = {};
let errorClass = {};
let formIsValid = true;
for(let i=1;i<=count;i++){
if (!this.state.formArray['name'+i]) {
formIsValid = false;
errors["name"+i] = "Name is required";
errorClass["name"+i] = "is-danger";
}
if (!this.state.formArray['email'+i]) {
formIsValid = false;
errors["email"+i] = "Email is required";
errorClass["email"+i] = "is-danger";
}
if (!this.state.formArray['contact'+i]) {
formIsValid = false;
errors["contact"+i] = "Contact is required";
errorClass["contact"+i] = "is-danger";
}
if (!this.state.formArray['designation'+i]) {
formIsValid = false;
errors["designation"+i] = "Designation is required";
errorClass["designaation"+i] = "is-danger";
}
if (!this.state.formArray['department'+i]) {
formIsValid = false;
errors["department"+i] = "Department is required";
errorClass["department"+i] = "is-danger";
}
if (!this.state.formArray['gender'+i]) {
formIsValid = false;
errors["gender"+i] = "Gender is required";
errorClass["gender"+i] = "is-danger";
}
}
this.setState({
errors: errors
});
this.setState({
errorClass: errorClass
});
return formIsValid;
}
render(){
if(this.props.List.department!==undefined && this.props.List.designation!==undefined ){
let dept= this.props.List.department.map(data=>(
<option key={data.id} value = {data.name} > {data.name} </option>
));
let desg=this.props.List.designation.map(data=>(
<option key={data.id} value={data.name}>{data.name}</option>
));
var html = MemberForm(this.props.formObj.count,dept,desg);
}
return (<div className="container has-text-center">
<form onSubmit={this.handleSubmit}>
<div className="columns">{html}</div>
<div className="columns field">
<button className="button is-success" type="submit">Submit</button>
</div>
</form>
</div>)
}
}
export const MemberForm=(i,dept,desg)=>{
let buffer=;
for (let j=1;j<=i;j++){
buffer.push((<div className="card" key={j}>
<header className="card-header">
<p className="card-header-title">Member Detail-{j}</p>
</header>
<div className="card-content">
<div className="field">
<label className="label">Name</label>
<div className="control">
<input name={"name"+j} className={"input "+ (this.state.errorClass["name"+j])} type="text" placeholder="Name"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Email</label>
<div className="control">
<input name={"email"+j} className={"input "+ (this.state.errorClass["email"+j])} type="email" placeholder="Email"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Contact</label>
<div className="control">
<input name={"contact"+j} className={"input "+ (this.state.errorClass["contact"+j])} type="tel" placeholder="Contact"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Gender</label>
<div className="control">
<div className={"select " + (this.state.errorClass["gender" + j])}>
<select name={"gender" + j}><option value='' hidden>Select Gender</option>
<option value="m">Male</option>
<option value="f">Female</option>
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Designation</label>
<div className="control">
<div className={"select "+ (this.state.errorClass["designation"+j])}>
<select name={"designation"+j}><option value='' hidden>Select Designation</option>
{desg}
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Department</label>
<div className="control">
<div className={"select "+ (this.state.errorClass["department"+j])}>
<select name={"department"+j}><option value='' hidden>Select Department</option>
{dept}
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
</div > </div> ));
}
return buffer;
}
CollegeMember.propTypes = {
formObj: PropTypes.object.isRequired,
getDesignationDepartment: PropTypes.func.isRequired
}
const mapStateToProps = state => ({
formObj: state.apiData.formObj,
List:state.memData.List
});
export default connect(mapStateToProps, {
getDesignationDepartment
})(CollegeMember);
Getting Error :
TypeError: Cannot read property 'state' of undefined
on Line
<input name={"name"+j} className={"input "+ (this.state.errorClass["name"+j])} type="text" placeholder="Name"/>
reactjs react-redux
add a comment |
I am trying to code in the react for learning and building a dynamic form which takes values from the previous state and generates according to it.
The previous from take input count and I set to a state variable which below component uses it i.e fromObj.count
Below is the component of form :
import React,{ Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { getDesignationDepartment } from '../actions/fetchActions';
class CollegeMember extends Component{
constructor(props) {
super(props);
this.state= {
formArray : {},
errors:{},
errorClass:{}
}
}
componentDidMount(){
this.props.getDesignationDepartment();
}
handleSubmit = (e) => {
e.preventDefault();
if (this.handleValidation()) {
this.props.formData(e);
}
}
handleValidation = () => {
let count=this.props.formObj.count;
let errors = {};
let errorClass = {};
let formIsValid = true;
for(let i=1;i<=count;i++){
if (!this.state.formArray['name'+i]) {
formIsValid = false;
errors["name"+i] = "Name is required";
errorClass["name"+i] = "is-danger";
}
if (!this.state.formArray['email'+i]) {
formIsValid = false;
errors["email"+i] = "Email is required";
errorClass["email"+i] = "is-danger";
}
if (!this.state.formArray['contact'+i]) {
formIsValid = false;
errors["contact"+i] = "Contact is required";
errorClass["contact"+i] = "is-danger";
}
if (!this.state.formArray['designation'+i]) {
formIsValid = false;
errors["designation"+i] = "Designation is required";
errorClass["designaation"+i] = "is-danger";
}
if (!this.state.formArray['department'+i]) {
formIsValid = false;
errors["department"+i] = "Department is required";
errorClass["department"+i] = "is-danger";
}
if (!this.state.formArray['gender'+i]) {
formIsValid = false;
errors["gender"+i] = "Gender is required";
errorClass["gender"+i] = "is-danger";
}
}
this.setState({
errors: errors
});
this.setState({
errorClass: errorClass
});
return formIsValid;
}
render(){
if(this.props.List.department!==undefined && this.props.List.designation!==undefined ){
let dept= this.props.List.department.map(data=>(
<option key={data.id} value = {data.name} > {data.name} </option>
));
let desg=this.props.List.designation.map(data=>(
<option key={data.id} value={data.name}>{data.name}</option>
));
var html = MemberForm(this.props.formObj.count,dept,desg);
}
return (<div className="container has-text-center">
<form onSubmit={this.handleSubmit}>
<div className="columns">{html}</div>
<div className="columns field">
<button className="button is-success" type="submit">Submit</button>
</div>
</form>
</div>)
}
}
export const MemberForm=(i,dept,desg)=>{
let buffer=;
for (let j=1;j<=i;j++){
buffer.push((<div className="card" key={j}>
<header className="card-header">
<p className="card-header-title">Member Detail-{j}</p>
</header>
<div className="card-content">
<div className="field">
<label className="label">Name</label>
<div className="control">
<input name={"name"+j} className={"input "+ (this.state.errorClass["name"+j])} type="text" placeholder="Name"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Email</label>
<div className="control">
<input name={"email"+j} className={"input "+ (this.state.errorClass["email"+j])} type="email" placeholder="Email"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Contact</label>
<div className="control">
<input name={"contact"+j} className={"input "+ (this.state.errorClass["contact"+j])} type="tel" placeholder="Contact"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Gender</label>
<div className="control">
<div className={"select " + (this.state.errorClass["gender" + j])}>
<select name={"gender" + j}><option value='' hidden>Select Gender</option>
<option value="m">Male</option>
<option value="f">Female</option>
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Designation</label>
<div className="control">
<div className={"select "+ (this.state.errorClass["designation"+j])}>
<select name={"designation"+j}><option value='' hidden>Select Designation</option>
{desg}
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Department</label>
<div className="control">
<div className={"select "+ (this.state.errorClass["department"+j])}>
<select name={"department"+j}><option value='' hidden>Select Department</option>
{dept}
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
</div > </div> ));
}
return buffer;
}
CollegeMember.propTypes = {
formObj: PropTypes.object.isRequired,
getDesignationDepartment: PropTypes.func.isRequired
}
const mapStateToProps = state => ({
formObj: state.apiData.formObj,
List:state.memData.List
});
export default connect(mapStateToProps, {
getDesignationDepartment
})(CollegeMember);
Getting Error :
TypeError: Cannot read property 'state' of undefined
on Line
<input name={"name"+j} className={"input "+ (this.state.errorClass["name"+j])} type="text" placeholder="Name"/>
reactjs react-redux
add a comment |
I am trying to code in the react for learning and building a dynamic form which takes values from the previous state and generates according to it.
The previous from take input count and I set to a state variable which below component uses it i.e fromObj.count
Below is the component of form :
import React,{ Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { getDesignationDepartment } from '../actions/fetchActions';
class CollegeMember extends Component{
constructor(props) {
super(props);
this.state= {
formArray : {},
errors:{},
errorClass:{}
}
}
componentDidMount(){
this.props.getDesignationDepartment();
}
handleSubmit = (e) => {
e.preventDefault();
if (this.handleValidation()) {
this.props.formData(e);
}
}
handleValidation = () => {
let count=this.props.formObj.count;
let errors = {};
let errorClass = {};
let formIsValid = true;
for(let i=1;i<=count;i++){
if (!this.state.formArray['name'+i]) {
formIsValid = false;
errors["name"+i] = "Name is required";
errorClass["name"+i] = "is-danger";
}
if (!this.state.formArray['email'+i]) {
formIsValid = false;
errors["email"+i] = "Email is required";
errorClass["email"+i] = "is-danger";
}
if (!this.state.formArray['contact'+i]) {
formIsValid = false;
errors["contact"+i] = "Contact is required";
errorClass["contact"+i] = "is-danger";
}
if (!this.state.formArray['designation'+i]) {
formIsValid = false;
errors["designation"+i] = "Designation is required";
errorClass["designaation"+i] = "is-danger";
}
if (!this.state.formArray['department'+i]) {
formIsValid = false;
errors["department"+i] = "Department is required";
errorClass["department"+i] = "is-danger";
}
if (!this.state.formArray['gender'+i]) {
formIsValid = false;
errors["gender"+i] = "Gender is required";
errorClass["gender"+i] = "is-danger";
}
}
this.setState({
errors: errors
});
this.setState({
errorClass: errorClass
});
return formIsValid;
}
render(){
if(this.props.List.department!==undefined && this.props.List.designation!==undefined ){
let dept= this.props.List.department.map(data=>(
<option key={data.id} value = {data.name} > {data.name} </option>
));
let desg=this.props.List.designation.map(data=>(
<option key={data.id} value={data.name}>{data.name}</option>
));
var html = MemberForm(this.props.formObj.count,dept,desg);
}
return (<div className="container has-text-center">
<form onSubmit={this.handleSubmit}>
<div className="columns">{html}</div>
<div className="columns field">
<button className="button is-success" type="submit">Submit</button>
</div>
</form>
</div>)
}
}
export const MemberForm=(i,dept,desg)=>{
let buffer=;
for (let j=1;j<=i;j++){
buffer.push((<div className="card" key={j}>
<header className="card-header">
<p className="card-header-title">Member Detail-{j}</p>
</header>
<div className="card-content">
<div className="field">
<label className="label">Name</label>
<div className="control">
<input name={"name"+j} className={"input "+ (this.state.errorClass["name"+j])} type="text" placeholder="Name"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Email</label>
<div className="control">
<input name={"email"+j} className={"input "+ (this.state.errorClass["email"+j])} type="email" placeholder="Email"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Contact</label>
<div className="control">
<input name={"contact"+j} className={"input "+ (this.state.errorClass["contact"+j])} type="tel" placeholder="Contact"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Gender</label>
<div className="control">
<div className={"select " + (this.state.errorClass["gender" + j])}>
<select name={"gender" + j}><option value='' hidden>Select Gender</option>
<option value="m">Male</option>
<option value="f">Female</option>
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Designation</label>
<div className="control">
<div className={"select "+ (this.state.errorClass["designation"+j])}>
<select name={"designation"+j}><option value='' hidden>Select Designation</option>
{desg}
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Department</label>
<div className="control">
<div className={"select "+ (this.state.errorClass["department"+j])}>
<select name={"department"+j}><option value='' hidden>Select Department</option>
{dept}
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
</div > </div> ));
}
return buffer;
}
CollegeMember.propTypes = {
formObj: PropTypes.object.isRequired,
getDesignationDepartment: PropTypes.func.isRequired
}
const mapStateToProps = state => ({
formObj: state.apiData.formObj,
List:state.memData.List
});
export default connect(mapStateToProps, {
getDesignationDepartment
})(CollegeMember);
Getting Error :
TypeError: Cannot read property 'state' of undefined
on Line
<input name={"name"+j} className={"input "+ (this.state.errorClass["name"+j])} type="text" placeholder="Name"/>
reactjs react-redux
I am trying to code in the react for learning and building a dynamic form which takes values from the previous state and generates according to it.
The previous from take input count and I set to a state variable which below component uses it i.e fromObj.count
Below is the component of form :
import React,{ Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { getDesignationDepartment } from '../actions/fetchActions';
class CollegeMember extends Component{
constructor(props) {
super(props);
this.state= {
formArray : {},
errors:{},
errorClass:{}
}
}
componentDidMount(){
this.props.getDesignationDepartment();
}
handleSubmit = (e) => {
e.preventDefault();
if (this.handleValidation()) {
this.props.formData(e);
}
}
handleValidation = () => {
let count=this.props.formObj.count;
let errors = {};
let errorClass = {};
let formIsValid = true;
for(let i=1;i<=count;i++){
if (!this.state.formArray['name'+i]) {
formIsValid = false;
errors["name"+i] = "Name is required";
errorClass["name"+i] = "is-danger";
}
if (!this.state.formArray['email'+i]) {
formIsValid = false;
errors["email"+i] = "Email is required";
errorClass["email"+i] = "is-danger";
}
if (!this.state.formArray['contact'+i]) {
formIsValid = false;
errors["contact"+i] = "Contact is required";
errorClass["contact"+i] = "is-danger";
}
if (!this.state.formArray['designation'+i]) {
formIsValid = false;
errors["designation"+i] = "Designation is required";
errorClass["designaation"+i] = "is-danger";
}
if (!this.state.formArray['department'+i]) {
formIsValid = false;
errors["department"+i] = "Department is required";
errorClass["department"+i] = "is-danger";
}
if (!this.state.formArray['gender'+i]) {
formIsValid = false;
errors["gender"+i] = "Gender is required";
errorClass["gender"+i] = "is-danger";
}
}
this.setState({
errors: errors
});
this.setState({
errorClass: errorClass
});
return formIsValid;
}
render(){
if(this.props.List.department!==undefined && this.props.List.designation!==undefined ){
let dept= this.props.List.department.map(data=>(
<option key={data.id} value = {data.name} > {data.name} </option>
));
let desg=this.props.List.designation.map(data=>(
<option key={data.id} value={data.name}>{data.name}</option>
));
var html = MemberForm(this.props.formObj.count,dept,desg);
}
return (<div className="container has-text-center">
<form onSubmit={this.handleSubmit}>
<div className="columns">{html}</div>
<div className="columns field">
<button className="button is-success" type="submit">Submit</button>
</div>
</form>
</div>)
}
}
export const MemberForm=(i,dept,desg)=>{
let buffer=;
for (let j=1;j<=i;j++){
buffer.push((<div className="card" key={j}>
<header className="card-header">
<p className="card-header-title">Member Detail-{j}</p>
</header>
<div className="card-content">
<div className="field">
<label className="label">Name</label>
<div className="control">
<input name={"name"+j} className={"input "+ (this.state.errorClass["name"+j])} type="text" placeholder="Name"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Email</label>
<div className="control">
<input name={"email"+j} className={"input "+ (this.state.errorClass["email"+j])} type="email" placeholder="Email"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Contact</label>
<div className="control">
<input name={"contact"+j} className={"input "+ (this.state.errorClass["contact"+j])} type="tel" placeholder="Contact"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Gender</label>
<div className="control">
<div className={"select " + (this.state.errorClass["gender" + j])}>
<select name={"gender" + j}><option value='' hidden>Select Gender</option>
<option value="m">Male</option>
<option value="f">Female</option>
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Designation</label>
<div className="control">
<div className={"select "+ (this.state.errorClass["designation"+j])}>
<select name={"designation"+j}><option value='' hidden>Select Designation</option>
{desg}
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Department</label>
<div className="control">
<div className={"select "+ (this.state.errorClass["department"+j])}>
<select name={"department"+j}><option value='' hidden>Select Department</option>
{dept}
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
</div > </div> ));
}
return buffer;
}
CollegeMember.propTypes = {
formObj: PropTypes.object.isRequired,
getDesignationDepartment: PropTypes.func.isRequired
}
const mapStateToProps = state => ({
formObj: state.apiData.formObj,
List:state.memData.List
});
export default connect(mapStateToProps, {
getDesignationDepartment
})(CollegeMember);
Getting Error :
TypeError: Cannot read property 'state' of undefined
on Line
<input name={"name"+j} className={"input "+ (this.state.errorClass["name"+j])} type="text" placeholder="Name"/>
reactjs react-redux
reactjs react-redux
edited Nov 16 '18 at 7:08
Zain Farooq
1,99821030
1,99821030
asked Nov 16 '18 at 6:52
Sharad MishraSharad Mishra
247
247
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
MemberForm is a function and won't have access to this referring to the class component in React. You either pass state as an argument to this function or define it as a function without using arrow function so that you can call it with an explicit this
First approach
var html = MemberForm(this.props.formObj.count,dept,desg, this.state.errorClass);
export const MemberForm=(i,dept,desg, errorClass)=>{
let buffer=;
for (let j=1;j<=i;j++){
buffer.push((<div className="card" key={j}>
<header className="card-header">
<p className="card-header-title">Member Detail-{j}</p>
</header>
<div className="card-content">
<div className="field">
<label className="label">Name</label>
<div className="control">
<input name={"name"+j} className={"input "+ (errorClass["name"+j])} type="text" placeholder="Name"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Email</label>
<div className="control">
<input name={"email"+j} className={"input "+ (errorClass["email"+j])} type="email" placeholder="Email"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Contact</label>
<div className="control">
<input name={"contact"+j} className={"input "+ (errorClass["contact"+j])} type="tel" placeholder="Contact"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Gender</label>
<div className="control">
<div className={"select " + (errorClass["gender" + j])}>
<select name={"gender" + j}><option value='' hidden>Select Gender</option>
<option value="m">Male</option>
<option value="f">Female</option>
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Designation</label>
<div className="control">
<div className={"select "+ (errorClass["designation"+j])}>
<select name={"designation"+j}><option value='' hidden>Select Designation</option>
{desg}
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Department</label>
<div className="control">
<div className={"select "+ (errorClass["department"+j])}>
<select name={"department"+j}><option value='' hidden>Select Department</option>
{dept}
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
</div > </div> ));
}
return buffer;
}
Second approach
export function MemberForm(i,dept,desg) {
and using it like
var html = MemberForm.call(this, this.props.formObj.count,dept,desg, this.state.errorClass);
isnt arrow function sort out "this" problem?
– Sharad Mishra
Nov 16 '18 at 7:02
I still don't get why I can't use outside "this" inside arrow function, Can you point it out why?
– Sharad Mishra
Nov 16 '18 at 7:10
1
This inside arrow function will refer to its enclosing scope, which in your case in nothing.
– Shubham Khatri
Nov 16 '18 at 7:18
1
creating arrow functions inside class render method will create a new instance of it everytime, so you must be carefull with this approach as it can have memory issues
– Shubham Khatri
Nov 16 '18 at 7:27
1
outside render but in class scope
– Sharad Mishra
Nov 16 '18 at 10:11
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332805%2freact-building-dynamic-form-from-previous-state-and-handling-error-on-it-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
MemberForm is a function and won't have access to this referring to the class component in React. You either pass state as an argument to this function or define it as a function without using arrow function so that you can call it with an explicit this
First approach
var html = MemberForm(this.props.formObj.count,dept,desg, this.state.errorClass);
export const MemberForm=(i,dept,desg, errorClass)=>{
let buffer=;
for (let j=1;j<=i;j++){
buffer.push((<div className="card" key={j}>
<header className="card-header">
<p className="card-header-title">Member Detail-{j}</p>
</header>
<div className="card-content">
<div className="field">
<label className="label">Name</label>
<div className="control">
<input name={"name"+j} className={"input "+ (errorClass["name"+j])} type="text" placeholder="Name"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Email</label>
<div className="control">
<input name={"email"+j} className={"input "+ (errorClass["email"+j])} type="email" placeholder="Email"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Contact</label>
<div className="control">
<input name={"contact"+j} className={"input "+ (errorClass["contact"+j])} type="tel" placeholder="Contact"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Gender</label>
<div className="control">
<div className={"select " + (errorClass["gender" + j])}>
<select name={"gender" + j}><option value='' hidden>Select Gender</option>
<option value="m">Male</option>
<option value="f">Female</option>
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Designation</label>
<div className="control">
<div className={"select "+ (errorClass["designation"+j])}>
<select name={"designation"+j}><option value='' hidden>Select Designation</option>
{desg}
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Department</label>
<div className="control">
<div className={"select "+ (errorClass["department"+j])}>
<select name={"department"+j}><option value='' hidden>Select Department</option>
{dept}
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
</div > </div> ));
}
return buffer;
}
Second approach
export function MemberForm(i,dept,desg) {
and using it like
var html = MemberForm.call(this, this.props.formObj.count,dept,desg, this.state.errorClass);
isnt arrow function sort out "this" problem?
– Sharad Mishra
Nov 16 '18 at 7:02
I still don't get why I can't use outside "this" inside arrow function, Can you point it out why?
– Sharad Mishra
Nov 16 '18 at 7:10
1
This inside arrow function will refer to its enclosing scope, which in your case in nothing.
– Shubham Khatri
Nov 16 '18 at 7:18
1
creating arrow functions inside class render method will create a new instance of it everytime, so you must be carefull with this approach as it can have memory issues
– Shubham Khatri
Nov 16 '18 at 7:27
1
outside render but in class scope
– Sharad Mishra
Nov 16 '18 at 10:11
|
show 1 more comment
MemberForm is a function and won't have access to this referring to the class component in React. You either pass state as an argument to this function or define it as a function without using arrow function so that you can call it with an explicit this
First approach
var html = MemberForm(this.props.formObj.count,dept,desg, this.state.errorClass);
export const MemberForm=(i,dept,desg, errorClass)=>{
let buffer=;
for (let j=1;j<=i;j++){
buffer.push((<div className="card" key={j}>
<header className="card-header">
<p className="card-header-title">Member Detail-{j}</p>
</header>
<div className="card-content">
<div className="field">
<label className="label">Name</label>
<div className="control">
<input name={"name"+j} className={"input "+ (errorClass["name"+j])} type="text" placeholder="Name"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Email</label>
<div className="control">
<input name={"email"+j} className={"input "+ (errorClass["email"+j])} type="email" placeholder="Email"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Contact</label>
<div className="control">
<input name={"contact"+j} className={"input "+ (errorClass["contact"+j])} type="tel" placeholder="Contact"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Gender</label>
<div className="control">
<div className={"select " + (errorClass["gender" + j])}>
<select name={"gender" + j}><option value='' hidden>Select Gender</option>
<option value="m">Male</option>
<option value="f">Female</option>
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Designation</label>
<div className="control">
<div className={"select "+ (errorClass["designation"+j])}>
<select name={"designation"+j}><option value='' hidden>Select Designation</option>
{desg}
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Department</label>
<div className="control">
<div className={"select "+ (errorClass["department"+j])}>
<select name={"department"+j}><option value='' hidden>Select Department</option>
{dept}
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
</div > </div> ));
}
return buffer;
}
Second approach
export function MemberForm(i,dept,desg) {
and using it like
var html = MemberForm.call(this, this.props.formObj.count,dept,desg, this.state.errorClass);
isnt arrow function sort out "this" problem?
– Sharad Mishra
Nov 16 '18 at 7:02
I still don't get why I can't use outside "this" inside arrow function, Can you point it out why?
– Sharad Mishra
Nov 16 '18 at 7:10
1
This inside arrow function will refer to its enclosing scope, which in your case in nothing.
– Shubham Khatri
Nov 16 '18 at 7:18
1
creating arrow functions inside class render method will create a new instance of it everytime, so you must be carefull with this approach as it can have memory issues
– Shubham Khatri
Nov 16 '18 at 7:27
1
outside render but in class scope
– Sharad Mishra
Nov 16 '18 at 10:11
|
show 1 more comment
MemberForm is a function and won't have access to this referring to the class component in React. You either pass state as an argument to this function or define it as a function without using arrow function so that you can call it with an explicit this
First approach
var html = MemberForm(this.props.formObj.count,dept,desg, this.state.errorClass);
export const MemberForm=(i,dept,desg, errorClass)=>{
let buffer=;
for (let j=1;j<=i;j++){
buffer.push((<div className="card" key={j}>
<header className="card-header">
<p className="card-header-title">Member Detail-{j}</p>
</header>
<div className="card-content">
<div className="field">
<label className="label">Name</label>
<div className="control">
<input name={"name"+j} className={"input "+ (errorClass["name"+j])} type="text" placeholder="Name"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Email</label>
<div className="control">
<input name={"email"+j} className={"input "+ (errorClass["email"+j])} type="email" placeholder="Email"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Contact</label>
<div className="control">
<input name={"contact"+j} className={"input "+ (errorClass["contact"+j])} type="tel" placeholder="Contact"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Gender</label>
<div className="control">
<div className={"select " + (errorClass["gender" + j])}>
<select name={"gender" + j}><option value='' hidden>Select Gender</option>
<option value="m">Male</option>
<option value="f">Female</option>
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Designation</label>
<div className="control">
<div className={"select "+ (errorClass["designation"+j])}>
<select name={"designation"+j}><option value='' hidden>Select Designation</option>
{desg}
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Department</label>
<div className="control">
<div className={"select "+ (errorClass["department"+j])}>
<select name={"department"+j}><option value='' hidden>Select Department</option>
{dept}
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
</div > </div> ));
}
return buffer;
}
Second approach
export function MemberForm(i,dept,desg) {
and using it like
var html = MemberForm.call(this, this.props.formObj.count,dept,desg, this.state.errorClass);
MemberForm is a function and won't have access to this referring to the class component in React. You either pass state as an argument to this function or define it as a function without using arrow function so that you can call it with an explicit this
First approach
var html = MemberForm(this.props.formObj.count,dept,desg, this.state.errorClass);
export const MemberForm=(i,dept,desg, errorClass)=>{
let buffer=;
for (let j=1;j<=i;j++){
buffer.push((<div className="card" key={j}>
<header className="card-header">
<p className="card-header-title">Member Detail-{j}</p>
</header>
<div className="card-content">
<div className="field">
<label className="label">Name</label>
<div className="control">
<input name={"name"+j} className={"input "+ (errorClass["name"+j])} type="text" placeholder="Name"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Email</label>
<div className="control">
<input name={"email"+j} className={"input "+ (errorClass["email"+j])} type="email" placeholder="Email"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Contact</label>
<div className="control">
<input name={"contact"+j} className={"input "+ (errorClass["contact"+j])} type="tel" placeholder="Contact"/>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Gender</label>
<div className="control">
<div className={"select " + (errorClass["gender" + j])}>
<select name={"gender" + j}><option value='' hidden>Select Gender</option>
<option value="m">Male</option>
<option value="f">Female</option>
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Designation</label>
<div className="control">
<div className={"select "+ (errorClass["designation"+j])}>
<select name={"designation"+j}><option value='' hidden>Select Designation</option>
{desg}
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
<div className="field">
<label className="label">Select Department</label>
<div className="control">
<div className={"select "+ (errorClass["department"+j])}>
<select name={"department"+j}><option value='' hidden>Select Department</option>
{dept}
</select>
</div>
</div>
<p className="help is-danger"></p>
</div>
</div > </div> ));
}
return buffer;
}
Second approach
export function MemberForm(i,dept,desg) {
and using it like
var html = MemberForm.call(this, this.props.formObj.count,dept,desg, this.state.errorClass);
answered Nov 16 '18 at 6:58
Shubham KhatriShubham Khatri
93.2k15117158
93.2k15117158
isnt arrow function sort out "this" problem?
– Sharad Mishra
Nov 16 '18 at 7:02
I still don't get why I can't use outside "this" inside arrow function, Can you point it out why?
– Sharad Mishra
Nov 16 '18 at 7:10
1
This inside arrow function will refer to its enclosing scope, which in your case in nothing.
– Shubham Khatri
Nov 16 '18 at 7:18
1
creating arrow functions inside class render method will create a new instance of it everytime, so you must be carefull with this approach as it can have memory issues
– Shubham Khatri
Nov 16 '18 at 7:27
1
outside render but in class scope
– Sharad Mishra
Nov 16 '18 at 10:11
|
show 1 more comment
isnt arrow function sort out "this" problem?
– Sharad Mishra
Nov 16 '18 at 7:02
I still don't get why I can't use outside "this" inside arrow function, Can you point it out why?
– Sharad Mishra
Nov 16 '18 at 7:10
1
This inside arrow function will refer to its enclosing scope, which in your case in nothing.
– Shubham Khatri
Nov 16 '18 at 7:18
1
creating arrow functions inside class render method will create a new instance of it everytime, so you must be carefull with this approach as it can have memory issues
– Shubham Khatri
Nov 16 '18 at 7:27
1
outside render but in class scope
– Sharad Mishra
Nov 16 '18 at 10:11
isnt arrow function sort out "this" problem?
– Sharad Mishra
Nov 16 '18 at 7:02
isnt arrow function sort out "this" problem?
– Sharad Mishra
Nov 16 '18 at 7:02
I still don't get why I can't use outside "this" inside arrow function, Can you point it out why?
– Sharad Mishra
Nov 16 '18 at 7:10
I still don't get why I can't use outside "this" inside arrow function, Can you point it out why?
– Sharad Mishra
Nov 16 '18 at 7:10
1
1
This inside arrow function will refer to its enclosing scope, which in your case in nothing.
– Shubham Khatri
Nov 16 '18 at 7:18
This inside arrow function will refer to its enclosing scope, which in your case in nothing.
– Shubham Khatri
Nov 16 '18 at 7:18
1
1
creating arrow functions inside class render method will create a new instance of it everytime, so you must be carefull with this approach as it can have memory issues
– Shubham Khatri
Nov 16 '18 at 7:27
creating arrow functions inside class render method will create a new instance of it everytime, so you must be carefull with this approach as it can have memory issues
– Shubham Khatri
Nov 16 '18 at 7:27
1
1
outside render but in class scope
– Sharad Mishra
Nov 16 '18 at 10:11
outside render but in class scope
– Sharad Mishra
Nov 16 '18 at 10:11
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332805%2freact-building-dynamic-form-from-previous-state-and-handling-error-on-it-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown