gpt4 book ai didi

javascript - React-Redux - 创建搜索过滤器

转载 作者:搜寻专家 更新时间:2023-10-31 21:55:49 25 4
gpt4 key购买 nike

<分区>

我需要帮助在我的应用程序中制作搜索过滤器。这是一个简单的应用程序,用于学习目的。

目标是创建一个搜索过滤器。我在 search_bar 容器中有 state,我想我需要将它作为 props 传递给我的 work_list 容器。但我不知道该怎么做。从那个阶段我会把它变成这样。

work_list.js

renderWork() {

let filteredWorks = this.props.works.filter((work) => {
return work.title.indexOf(this.state.init) !== -1;
}
);


return filteredWorks.map((work) => {
return (
<tr>
<td>{work.title}</td>
</tr>
);
});
}

不知道这个逻辑对不对。需要帮助。

app.js

import React, { Component } from 'react';
import SearchBar from '../containers/search_bar';
import WorkList from '../containers/work_list.js';

export default class App extends Component {
render() {
return (
<div>
<SearchBar/>
<WorkList/>
</div>
);
}
}

search_bar.js

import React, { Component } from 'react';

export default class SearchBar extends Component {

constructor(props) {
super(props);
this.state = { init: '' };
this.onInputChange = this.onInputChange.bind(this);
}

onInputChange(event) {
this.setState({ init: event.target.value });
}

onFormSubmit(event) {
event.preventDefault();
}
render() {
return (
<form
onSubmit={this.onFormSubmit}
className="input-group">
<input
className="form-control"
placeholder = "Procurar Trabalho"
onChange={this.onInputChange}
value={this.state.init} />
<span className="input-group-btn">
<button type="submit" className="btn btn-secondary">Pesquisar</button>
</span>
</form>
);
}
}

work_list.js

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { fetchWork } from '../actions/index';

class WorkList extends Component {

renderWork() {
return this.props.works.map((work) => {
return (
<tr>
<td>{work.title}</td>
</tr>
);
});
}

render() {
return (
<table className="table table-hover">
<thead>
<tr>
<th>Nome</th>
</tr>
</thead>
<tbody>
{this.renderWork() }
</tbody>
</table>

);
}
}

function mapStateToProps(state) {
return {
works: state.works
}
}

export default connect(mapStateToProps)(WorkList);

我的 reducer

reducer_work.js

export default function () {
return [
{ title: 'Mirististica' },
{ title: 'Vet' }
];
}

index.js

import { combineReducers } from 'redux';
import MostrarTrabalhos from './reducer_work';

const rootReducer = combineReducers({
works : MostrarTrabalhos
});

export default rootReducer;

谢谢!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com