【发布时间】:2020-03-19 12:45:37
【问题描述】:
我有以下组件
import {h, Component} from 'preact'
import {getPersons} from '../../lib/datalayer'
import Person from '../person'
import {SearchInput} from '../search'
export default class Persons extends Component {
state = {
allPersons: [],
persons: [],
search: ''
}
async fetchData () {
try {
const allPersons = await getPersons()
this.setState({allPersons: allPersons.slice(), persons: allPersons.slice()})
} catch (error) {
....
}
}
constructor (props) {
super(props)
this.state = {
allPersons: [],
persons: [],
search: ''
}
this.fetchData()
}
onSearchInput = (search) => {
if (search === '') {
this.setState({search: search, persons: this.state.allPersons.slice()})
} else {
const persons = this.state.allPersons.filter(p => p.name.toLowerCase().includes(search.toLowerCase())).slice()
this.setState({search: search, persons: persons)})
}
}
render () {
const {persons} = this.state
return (
<div>
<SearchInput onInputChange={this.onSearchInput} placeHolder={'filter: name'} />
{persons.map(p => <Person person={p} />)}
</div>
)
}
}
页面呈现人员列表,顶部有一个过滤器。过滤器似乎工作正常,我通过控制台进行了测试。结果很好
问题是,如果我的列表包含对象:
[{name: 'thomas'}, {name: 'john'}, {name: 'marcus'}, {name: 'usa'}]
我在搜索输入中写:'us'
过滤器工作正常,结果是:
[{name: 'marcus'}, {name: 'usa'}] \\ (the expected result)
在页面中呈现这些对象
[{name: 'thomas'}, {name: 'john'}] \\ (wrong, this are the two first elements of the list)
如果我搜索:'joh'
过滤器的结果是
[{name: 'john'}] \\ (this is fine)
并且页面只呈现
[{name: 'thomas'}] \\ (the first element in the list)
看起来渲染的元素数量很好,但是列表的子元素的内容没有被重新渲染。
我的代码有什么问题?
【问题讨论】:
标签: javascript reactjs preact