admin管理员组文章数量:1405305
**EDIT: Please check the updated sandbox, it incorporates some of the changes suggested in the below answer. **
I'm using Formik and its <FieldArray ../>
ponent to handle some nested data to render inputs. I can't quite figure out what exact string to pass to the name prop in <FieldArray name={ // here}
.
In my ponent I have a user selection that filters out some data and returns this array.
"chosenDevice": [
{
"deviceName": "eth0",
"macAddress": "01:40:27:0F:2E:CB",
"ipv4DHCP": false,
"ipv4Addresses": [
"182.148.1.100/24"
],
"ipv4Gateway": "",
"ipv6DHCP": false,
"ipv6Addresses": [
"232.232.2/100/10"
],
"ipv6Gateway": ""
}
]
I want to access the ipv4Addresses
array. I'm either passing the wrong string to name
or not using index in .map
properly. Currently I have the following:
<FieldArray
name={`chosenDevice.ipv4Addresses[${index}]`}
>
{({ remove, push }) => (
<>
<div>
{values.chosenDevice.length > 0 &&
values.chosenDevice.map(
(ipv4Addresses, index) => (
<div
style={{
display: "flex"
}}
key={index}
>
<label
htmlFor={`chosenDevice[${index}].ipv4Addresses`}
className="custom-label"
style={{ flex: 1 }}
>
ipv4Address(es)
<Field
className="custom-input"
name={`chosenDevice[${index}].ipv4Addresses`}
placeholder="< ipv4Address >"
type="text"
/>
</label>
<ErrorMessage
name={`chosenDevice.${index}.ipv4Addresses`}
ponent="div"
className="field-error"
/>
I have a codesanbox that may provide some more context.
After looking into this further, basically I need to provide the string value of deviceName[0].ipv4Addresses[0]
, so I may need to rethink the .map and what it's actually returning.
**EDIT: Please check the updated sandbox, it incorporates some of the changes suggested in the below answer. **
I'm using Formik and its <FieldArray ../>
ponent to handle some nested data to render inputs. I can't quite figure out what exact string to pass to the name prop in <FieldArray name={ // here}
.
In my ponent I have a user selection that filters out some data and returns this array.
"chosenDevice": [
{
"deviceName": "eth0",
"macAddress": "01:40:27:0F:2E:CB",
"ipv4DHCP": false,
"ipv4Addresses": [
"182.148.1.100/24"
],
"ipv4Gateway": "",
"ipv6DHCP": false,
"ipv6Addresses": [
"232.232.2/100/10"
],
"ipv6Gateway": ""
}
]
I want to access the ipv4Addresses
array. I'm either passing the wrong string to name
or not using index in .map
properly. Currently I have the following:
<FieldArray
name={`chosenDevice.ipv4Addresses[${index}]`}
>
{({ remove, push }) => (
<>
<div>
{values.chosenDevice.length > 0 &&
values.chosenDevice.map(
(ipv4Addresses, index) => (
<div
style={{
display: "flex"
}}
key={index}
>
<label
htmlFor={`chosenDevice[${index}].ipv4Addresses`}
className="custom-label"
style={{ flex: 1 }}
>
ipv4Address(es)
<Field
className="custom-input"
name={`chosenDevice[${index}].ipv4Addresses`}
placeholder="< ipv4Address >"
type="text"
/>
</label>
<ErrorMessage
name={`chosenDevice.${index}.ipv4Addresses`}
ponent="div"
className="field-error"
/>
I have a codesanbox that may provide some more context.
After looking into this further, basically I need to provide the string value of deviceName[0].ipv4Addresses[0]
, so I may need to rethink the .map and what it's actually returning.
3 Answers
Reset to default 4With a .map
inside of another .map
, you might be getting index
values confused. Try naming index
something more specific to each .map
(such as deviceIndex
and addressIndex
). I've used addressIndex
for the inner map operation in my answer below.
It seems like your <FieldArray>
here should only concern itself with the ipv4Addresses
array on the current chosenDevice
object.
Try this:
<FieldArray
name="chosenDevice.ipv4Addresses"
>
{({ remove, push }) => (
<>
<div>
{chosenDevice.ipv4Addresses > 0 &&
chosenDevice.ipv4Addresses.map(
(ipv4Address, addressIndex) => (
<div
style={{
display: "flex"
}}
key={addressIndex}
>
<label
htmlFor={`chosenDevice.ipv4Addresses.${addressIndex}`}
className="custom-label"
style={{ flex: 1 }}
>
ipv4Address(es)
<Field
className="custom-input"
name={`chosenDevice.ipv4Addresses.${addressIndex}`}
placeholder="< ipv4Address >"
type="text"
/>
</label>
<ErrorMessage
name={`chosenDevice.ipv4Addresses.${addressIndex}`}
ponent="div"
className="field-error"
/>
But if I'm wrong about that, and the <FieldArray>
tag does need to reference which chosenDevice
it's a child of, this should do it:
<FieldArray
name={`chosenDevice.${index}.ipv4Addresses`}
>
{({ remove, push }) => (
<>
<div>
{chosenDevice[index].ipv4Addresses > 0 &&
chosenDevice[index].ipv4Addresses.map(
(ipv4Address, addressIndex) => (
<div
style={{
display: "flex"
}}
key={addressIndex}
>
<label
htmlFor={`chosenDevice.${index}.ipv4Addresses.${addressIndex}`}
className="custom-label"
style={{ flex: 1 }}
>
ipv4Address(es)
<Field
className="custom-input"
name={`chosenDevice.${index}.ipv4Addresses.${addressIndex}`}
placeholder="< ipv4Address >"
type="text"
/>
</label>
<ErrorMessage
name={`chosenDevice.${index}.ipv4Addresses.${addressIndex}`}
ponent="div"
className="field-error"
/>
<FieldArray name={`chosenDevice.ipv4Addresses[${index}]`}>
The index
here if you are trying to use from the map below, then this is wrong you'll not get index outside the map.
Use this map above the FieldArray
, like this:
values.chosenDevice.map((ipv4Addresses, index) => (
<FieldArray
name={`ipv4Addresses[${index}]`}
>
{({ remove, push }) => (
<>
<div>
<div
style={{
display: "flex"
}}
key={index}
>
<label
htmlFor={`chosenDevice[${index}].ipv4Addresses`}
className="custom-label"
style={{ flex: 1 }}
>
ipv4Address(es)
<Field
className="custom-input"
name={`chosenDevice[${index}].ipv4Addresses`}
placeholder="< ipv4Address >"
type="text"
/>
</label>
<ErrorMessage
name={`chosenDevice.${index}.ipv4Addresses`}
ponent="div"
className="field-error"
/>)
const [configIdx, setConfigIdx] = useState(0);
<FieldArray
name={`chosenDevice.ipv4Addresses[${configIdx}]`}
>
{({ remove, push }) => (
<>
<div>
{values.chosenDevice.length > 0 &&
values.chosenDevice.map(
(ipv4Addresses, index) => {
setConfigIdx(index);
return (
<div
style={{
display: "flex"
}}
key={index}
>
<label
See how I updating state, this might help to achieve your result.
本文标签: javascriptAccessing nested array in Formik FieldArrayStack Overflow
版权声明:本文标题:javascript - Accessing nested array in Formik FieldArray - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744900850a2631337.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论