Javascript reactjs中搜索功能的实现

Javascript reactjs中搜索功能的实现,javascript,node.js,reactjs,search,drop-down-menu,Javascript,Node.js,Reactjs,Search,Drop Down Menu,我正在尝试在reactjs中实现搜索功能。我不知道该怎么做。下面是我尝试过的代码。 我需要在serach之后在表中显示结果 render() { return ( <div> <input onChange={this.handleSear

我正在尝试在reactjs中实现搜索功能。我不知道该怎么做。下面是我尝试过的代码。
我需要在serach之后在表中显示结果

                        render() { 
                            return (
                                <div>
                                    <input onChange={this.handleSearchChange} placeholder="Search"/>
                                </div>  
                                )
                        }
//下面是我的api响应

              [
            {color: "green",name: "test",age: "22"},
            {color: "red",name: "test2",age: "23"}
        ]

一旦您有了数据,您需要将其添加到状态,以便当状态更改时,您可以迭代数据并重新渲染视图。我在这个例子中使用了React钩子。我希望这会有点帮助

表{
背景色:白色;
边界塌陷:塌陷;
}
tr:n个孩子(偶数){
背景色:#EFEF;
}
运输署{
填充:1em;
}

//抓住你需要的钩子
const{useState,useffect}=React;
//将在数据上循环的主函数
//并为表创建行
函数createRows(数据){
//迭代每个对象的数据和属性
//返回一个新的HTML行
返回data.map((行,i)=>{
const{color,name,age}=行;
返回(
{color}
{name}
{age}
)
});
}
//模拟API调用,延迟2秒后返回数据
函数fakeAPICall(){
返回新承诺((res,rej)=>{
设置超时(()=>{
res(“[{”color:“green”,“name:“test”,“age:“22”},{”color:“red”,“name:“test2”,“age:“23”}”);
}, 2000);
});
}
函数示例(){
//在组件中设置状态
const[data,setData]=useState([]);
//组件在数据中呈现负载时
//并将其设置为您的状态。
useffect(()=>{
异步函数getData(){
const response=wait fakeAPICall();
const data=JSON.parse(响应);
setData(数据);
}
getData();
}, []);
//如果状态中没有数据,则不显示任何内容。。。
如果(!data.length)不返回任何数据
//…否则,将数据传递到createRows函数中
//并将行数据返回给他们
返回(
颜色
名称
年龄
{createRows(数据)}
)
};
//渲染它
ReactDOM.render(
,
document.getElementById(“react”)
);

如果我正确理解了您的问题,您正在尝试更新表呈现。正如您所提到的,我将假设您的API在调用时运行良好

在React中,有一个称为“state”的特性,它可以保留变量状态。最酷的是,当你改变状态时,你的组件会重新渲染。一点解释或说明:

// intialization of a variable "varX" of type integer with value 1
// setVarX(int) is used to change the variable state
const [varX, setVarX] = React.useState(1);

// changing the state
setVarX(3);
因此,对于您的问题,我们需要两件事:一个保存API响应的状态,我们会在API有新数据时进行更新,另一个是数据的表显示

状态

在正在呈现的函数中(我假设它是name TableView),让我们添加此状态,并在API成功时在处理程序中更新它

function TableView(){

     // table data, initialized to empty array
     const [tableData, setTableData] = React.useState([]);

     // handle updated with set state
     handleSearchChange = e => {
        const { value } = e.target;
        var self = this.axios.post("http://localhost:4000/get",  { name:   value })
        .then(function(res){
            console.log("detail",res.data)
            setTableData(res.data) // update the table data!
        })
        .catch(function(err){
            console.log('Error',err)
        })  
    };
     
    return (...) // render function in the next section
}
渲染

该功能将使用react的映射功能:

render() { 
    return (
        <div>
            <input onChange={this.handleSearchChange} placeholder="Search"/>
            <table style="width:100%">
              <tr>
                <th>Color</th>
                <th>Name</th> 
                <th>Age</th>
              </tr>
            </table>
            {
                tableData.map((entry, index) => 
                    <tr index={index}>
                        <td>{entry.color}</td>
                        <td>{entry.name}</td> 
                        <td>{entry.age}</td>
                    </tr>
                )
            }
        </div>  
    )
}
render(){
返回(
颜色
名称
年龄
{
tableData.map((条目,索引)=>
{entry.color}
{entry.name}
{entry.age}
)
}
)
}

PS:我不是JSX的高手请随意编辑和增强渲染部分

你检查过了吗?到底是什么不起作用?你的请求有效吗?是的,请求有效,我得到了回复。我已经提到了上面的回答。我是reactjs的新手。。我正在使用类组件渲染表。这是错误的。我怎样才能把这个写进我的课堂component@kp987987,我用一个类组件示例更新了我的答案。
render() { 
    return (
        <div>
            <input onChange={this.handleSearchChange} placeholder="Search"/>
            <table style="width:100%">
              <tr>
                <th>Color</th>
                <th>Name</th> 
                <th>Age</th>
              </tr>
            </table>
            {
                tableData.map((entry, index) => 
                    <tr index={index}>
                        <td>{entry.color}</td>
                        <td>{entry.name}</td> 
                        <td>{entry.age}</td>
                    </tr>
                )
            }
        </div>  
    )
}