you need to get function with the index you found;
var func = functions.indexOf(options.indexOf(this.state.type));// this returns index not the actual func
functions[func] && functionsfunc
My Approach would be like;
getBTC = () => {
// .....
};
getETH = () => {
// .....
};
getADA = () => {
// .....
};
getCoin = (type) => {
switch(type) {
case "BTC": this.getBTC()
return
case "ADA":...
...
...
}
componentDidMount() {
this.getCoin(this.state.type)
}
Answer from ilkerkaran on Stack Overflowjavascript - How to call function from function array (React js) - Stack Overflow
javascript - Reactjs - How to pass an array to a function? - Stack Overflow
javascript - React : how to pass and array from inside a Function to the return (JSX) - Stack Overflow
javascript - How to map function returning array in react - Stack Overflow
You just initialise input as a string so just keep input for keeping input value not result data. You can create another state for keeping result OR put result data back on Data variable.
Here I am showing you to keep result data separate.
import React, { useState, useEffect } from "react";
import ListItem from "./ListItem";
const List = () => {
const [data, setData] = useState();
const [searchResult, setSearchResult] = useState();
const [input, setInput] = useState("");
const onInputChange = (event) => {
setInput(event.target.value);
const value = event.target.value.toLowerCase();
let result = [];
result = data.filter((item) =>
item.name.toLowerCase().includes(value.toLowerCase())
);
setSearchResult(result);
};
useEffect(() => {
const getData = async () => {
const response = await fetch(
"https://rickandmortyapi.com/api/character/"
);
const obj = await response.json();
setData(obj.results);
};
getData();
}, []);
return (
<div>
<input type="text" name={input} onChange={onInputChange}></input>
{input===""? data &&
data.map((item) => {
return <ListItem key={item.id} character={item} />;
}):
searchResult &&
searchResult.map((item) => {
return <ListItem key={item.id} character={item} />;
})
}
</div>
);
};
export default List;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
This is separating your original data and search result different.
You need to use a variable to store data after filter:
const [data, setData] = useState([]);
const onInputChange = (event) => {
setInput(event.target.value);
};
const result = data.filter((item) =>
item.name.toLowerCase().includes(input.toLowerCase())
);
return (
...
{result?.map((item) => {
<ListItem key={item.id} character={item} />;
})}
...
)
Data can be passed to components via props.
https://facebook.github.io/react/tutorial/tutorial.html#passing-data-through-props
In your case props would be accessed inside the components via this.props.
<TodoList /> takes a prop called items which is an array. Inside <TodoList /> you can map through that array and return elements.
For example in the render method of your class you would return TodoList with a prop of items:
const myItems = [{ name: 'item 1' }, { name: 'item2' }];
function MyApp() {
return (
<TodoList items={myItems} />
);
}
Then in TodoList you map the items
function TodoList({ items }) {
return items.map(item => (
<h1>{item.name}</h1>
));
}
You just set up an attribute, with the same name
<ToDoList items={myitems} />