While working with Kendo React, I wanted to add a Kendo DropDownList. I tried to fill the list with data like this:
Array(8) [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…} ]
0: Object { id: -1, name: null }
1: Object { id: 1, name: "A" }
2: Object { id: 2, name: "B" }
3: Object { id: 3, name: "C" }
4: Object { id: 4, name: "D" }
5: Object { id: 5, name: "E" }
6: Object { id: 6, name: "F" }
7: Object { id: 77, name: "G" }
The data is fetched with axios and put inside useState in UseEffect:
const[sektor, setSektor] = useState([]);
...
...
useEffect(() => {
(async() =>{
try{
const resSektor = await services.getSektor();
console.log(resSektor.data.rlista); //JSON above is this console log
setSektor(resSektor.data.rlista);
}
catch(e){
console.log(e);
}
})();
}, []);
So, 'sektor' is used as an array of objects that will fill the Kendo dropdown:
<DropDownList data={sektor} value={sektorV} style={{marginTop:'2%'}} onChange={handleChangeSektor} textField="name" dataItemKey="id"/>
...
...
const handleChangeSektor = (event) => {
setSektorV(event.target.value);
};
'sektorV' is a state that saves the object that is picked from the KendoDropDown and is defined like this:
const[sektorV, setSektorV] = useState({id: -1, name: "All"});
Everything seems to be correct, but when I click on the DropDown the screen goes white and this is console logged:
Uncaught TypeError: r.current.contains is not a function
...
Uncaught TypeError: _utils__WEBPACK_IMPORTED_MODULE_2__.getItemValue(...) is null
I had a bunch of dropdowns in my current project and they all worked fine. Can anybody see the problem?
CodePudding user response:
Just a hunch based on this limited code context, but I think this error is thrown because the first element has a null
name
property in the resSektor.data.rlista
array.
Try setSektor(resSektor.data.rlista.slice(1));
to remove the null
value or instead of null
update that element's name
property to "ALL"
to match the initial sectorV
state.
useEffect(() => {
(async() =>{
try {
const resSektor = await services.getSektor();
resSektor.data.rlista[0].name = "All";
setSektor(resSektor.data.rlista);
} catch(e) {
console.log(e);
}
})();
}, []);