So I want when the user clicks on the button, be able to create new CatagoryField
Component that I made. When I place the component in a function and call it it will only create the component once. I will appreciate some help. I’m confused about how should I implement this?
App components
JavaScript
x
201
201
1
import React, { useState } from "react";
2
import {
3
Accordion,
4
AccordionSummary,
5
Chip,
6
Button,
7
IconButton,
8
Autocomplete,
9
TextField,
10
} from "@mui/material";
11
import { Grid } from "@mui/material";
12
import SearchBar from "material-ui-search-bar";
13
import DeleteIcon from "@mui/icons-material/Delete";
14
import ExpandMoreOutlinedIcon from "@mui/icons-material/ExpandMoreOutlined";
15
import AddCircleOutlineOutlinedIcon from "@mui/icons-material/AddCircleOutlineOutlined";
16
import HelpIcon from "@mui/icons-material/Help";
17
import HistoryToggleOffIcon from "@mui/icons-material/HistoryToggleOff";
18
import AllMembers from "./components/common/main/allMembers";
19
import CatagoryField from "./components/common/main/catagoryField";
20
21
const autoCompleteOptions = [
22
{ title: "A", year: 1994 },
23
{ title: "B", year: 1972 },
24
{ title: "C", year: 1974 },
25
{ title: "D", year: 2008 },
26
{ title: "E", year: 1957 },
27
{ title: "F", year: 1993 },
28
{ title: "G", year: 1994 },
29
];
30
31
const App = () => {
32
const [value, setValue] = useState();
33
const [element, setElement] = useState(0);
34
35
const doSomethingWith = () => {
36
return null;
37
};
38
39
let i = 0;
40
const creator = () => {
41
while (i < element) {
42
i++;
43
return (
44
<CatagoryField
45
catagoryName="خدماتی"
46
react="از آنها چیزی میخریم"
47
createdBy="سیستم"
48
disable={false}
49
/>
50
);
51
}
52
};
53
54
console.log(element);
55
return (
56
<>
57
<div style={{ margin: "4rem 1rem" }}>
58
<Accordion>
59
<AccordionSummary
60
sx={{ height: "65px" }}
61
aria-controls="panel1a-content"
62
>
63
<Grid
64
container
65
direction="row"
66
justifyContent="space-between"
67
alignItems="center"
68
>
69
<Grid item>
70
<IconButton className="chevron__icon">
71
<ExpandMoreOutlinedIcon />
72
</IconButton>
73
<span className="users__catagory"> دسته بندی کاربران</span>
74
</Grid>
75
<Grid item>
76
<IconButton>
77
<HistoryToggleOffIcon />
78
</IconButton>
79
<IconButton>
80
<HelpIcon className="help" />
81
</IconButton>
82
<span className="version">4.3.2</span>
83
</Grid>
84
</Grid>
85
</AccordionSummary>
86
<AccordionSummary
87
sx={{
88
height: "65px",
89
padding: "30px",
90
background: "#E6E6E6",
91
cursor: "default !important",
92
}}
93
aria-controls="panel1a-content"
94
>
95
<Grid
96
container
97
direction="row"
98
alignItems="center"
99
sx={{ display: "relative" }}
100
>
101
<Grid item>
102
<Button
103
variant="outlined"
104
sx={{ height: "50px" }}
105
size="medium"
106
onClick={() => setElement(element + 1)}
107
endIcon={
108
<AddCircleOutlineOutlinedIcon
109
sx={{ marginRight: "10px" }}
110
/>
111
}
112
>
113
افزودن دسته جدید
114
</Button>
115
</Grid>
116
<Grid sx={{ width: "207px" }} item>
117
<SearchBar
118
className="search__holder"
119
placeholder="جستجو..."
120
value={value}
121
style={{ width: "207px", height: "45px" }}
122
onChange={(newValue) => setValue(newValue)}
123
onRequestSearch={() => doSomethingWith()}
124
/>
125
</Grid>
126
<Grid className="sort__field" item>
127
<Autocomplete
128
sx={{
129
width: "207px !important",
130
padding: "0 !important",
131
backgroundColor: "white !important",
132
}}
133
options={autoCompleteOptions}
134
getOptionLabel={(option) => option.title}
135
renderTags={(value, getTagProps) =>
136
value.map((option, index) => (
137
<Chip
138
variant="outlined"
139
label={option.title}
140
{getTagProps({ index })}
141
/>
142
))
143
}
144
renderInput={(params) => (
145
<TextField
146
{params}
147
variant="filled"
148
sx={{
149
width: "207px !important",
150
padding: "0 !important",
151
background: "white !important",
152
}}
153
placeholder="مرتب سازی..."
154
/>
155
)}
156
/>
157
</Grid>
158
<Grid item>
159
<IconButton sx={{ margin: "0 5px" }}>
160
<DeleteIcon />
161
</IconButton>
162
</Grid>
163
<Grid item className="last__edit">
164
<Grid item>
165
<span>
166
آخرین ویرایش:
167
<a className="last_Modified" href="#">
168
سیستم
169
</a>
170
</span>
171
<span>1405/12/24 23:59 </span>
172
</Grid>
173
</Grid>
174
</Grid>
175
</AccordionSummary>
176
<AllMembers />
177
<CatagoryField
178
catagoryName="اپراتور سیستم"
179
react="اپراتور سیستم"
180
createdBy="سیستم"
181
/>
182
<CatagoryField
183
catagoryName="مشتریان"
184
react="به آنها چیزی فروخته ایم"
185
createdBy="سیستم"
186
/>
187
<CatagoryField
188
catagoryName="خدماتی"
189
react="از آنها چیزی میخریم"
190
createdBy="سیستم"
191
disable={false}
192
/>
193
{creator()}
194
</Accordion>
195
</div>
196
</>
197
);
198
};
199
200
export default App;
201
Advertisement
Answer
The best way in order to achieve the solution is to use a state with useMemo.and the problem with your code is that the creator acts as a function and it will only execute once try this.
JavaScript
1
168
168
1
import {useMemo} from 'react';
2
const App = () => {
3
const [elements , setElements] = useState(0);
4
const creator = useMemo(() => {
5
return (
6
<>
7
{Array(elements).fill(elements).map((item , index) => (
8
<CategoryField
9
key={`cat-${index}`} // key is need when mapping! it is not props
10
catagoryName="خدماتی"
11
react="از آنها چیزی میخریم"
12
createdBy="سیستم"
13
disable={false}
14
15
/>
16
))}
17
</>
18
)
19
},[elements]);//render when elements change
20
21
return (
22
<>
23
<div style={{ margin: "4rem 1rem" }}>
24
<Accordion>
25
<AccordionSummary
26
sx={{ height: "65px" }}
27
aria-controls="panel1a-content"
28
>
29
<Grid
30
container
31
direction="row"
32
justifyContent="space-between"
33
alignItems="center"
34
>
35
<Grid item>
36
<IconButton className="chevron__icon">
37
<ExpandMoreOutlinedIcon />
38
</IconButton>
39
<span className="users__catagory"> دسته بندی کاربران</span>
40
</Grid>
41
<Grid item>
42
<IconButton>
43
<HistoryToggleOffIcon />
44
</IconButton>
45
<IconButton>
46
<HelpIcon className="help" />
47
</IconButton>
48
<span className="version">4.3.2</span>
49
</Grid>
50
</Grid>
51
</AccordionSummary>
52
<AccordionSummary
53
sx={{
54
height: "65px",
55
padding: "30px",
56
background: "#E6E6E6",
57
cursor: "default !important",
58
}}
59
aria-controls="panel1a-content"
60
>
61
<Grid
62
container
63
direction="row"
64
alignItems="center"
65
sx={{ display: "relative" }}
66
>
67
<Grid item>
68
<Button
69
variant="outlined"
70
sx={{ height: "50px" }}
71
size="medium"
72
onClick={() => setElements(elements + 1)}
73
endIcon={
74
<AddCircleOutlineOutlinedIcon
75
sx={{ marginRight: "10px" }}
76
/>
77
}
78
>
79
افزودن دسته جدید
80
</Button>
81
</Grid>
82
<Grid sx={{ width: "207px" }} item>
83
<SearchBar
84
className="search__holder"
85
placeholder="جستجو..."
86
value={value}
87
style={{ width: "207px", height: "45px" }}
88
onChange={(newValue) => setValue(newValue)}
89
onRequestSearch={() => doSomethingWith()}
90
/>
91
</Grid>
92
<Grid className="sort__field" item>
93
<Autocomplete
94
sx={{
95
width: "207px !important",
96
padding: "0 !important",
97
backgroundColor: "white !important",
98
}}
99
options={autoCompleteOptions}
100
getOptionLabel={(option) => option.title}
101
renderTags={(value, getTagProps) =>
102
value.map((option, index) => (
103
<Chip
104
key={`chip-${index}`}
105
variant="outlined"
106
label={option.title}
107
{...getTagProps({ index })}
108
/>
109
))
110
}
111
renderInput={(params) => (
112
<TextField
113
{...params}
114
variant="filled"
115
sx={{
116
width: "207px !important",
117
padding: "0 !important",
118
background: "white !important",
119
}}
120
placeholder="مرتب سازی..."
121
/>
122
)}
123
/>
124
</Grid>
125
<Grid item>
126
<IconButton sx={{ margin: "0 5px" }}>
127
<DeleteIcon />
128
</IconButton>
129
</Grid>
130
<Grid item className="last__edit">
131
<Grid item>
132
<span>
133
آخرین ویرایش:
134
<a className="last_Modified" href="#">
135
سیستم
136
</a>
137
</span>
138
<span>1405/12/24 23:59 </span>
139
</Grid>
140
</Grid>
141
</Grid>
142
</AccordionSummary>
143
<AllMembers />
144
<CatagoryField
145
catagoryName="اپراتور سیستم"
146
react="اپراتور سیستم"
147
createdBy="سیستم"
148
/>
149
<CatagoryField
150
catagoryName="مشتریان"
151
react="به آنها چیزی فروخته ایم"
152
createdBy="سیستم"
153
/>
154
<CatagoryField
155
catagoryName="خدماتی"
156
react="از آنها چیزی میخریم"
157
createdBy="سیستم"
158
disable={false}
159
/>
160
{creator}
161
</Accordion>
162
</div>
163
</>
164
);
165
166
}
167
export default App;
168