連絡先を表示するテーブルがあり、連絡先をファーストネームで並べ替える必要があります。連絡先配列はreduxストアから来ていますが、これは小道具を経由して来ますが、ローカルのUI状態であるため、ローカルの状態でその連絡先のソート方法を保持する必要があります。これをどのように達成するのですか?私は今のところ連絡先をcomponentWillReceiveProps
に置いていますが、何らかの理由でそれが変わったときにその小道具を受け取っていません。 reducexストアの状態が変わるたびに、ローカル状態を更新するにはどうすればよいですか?私が今見ている問題はcontactRows, filteredSortedContacts, sortedContacts
が一回ごとのTableRowのために、複数回呼び出されていることである同じ反応成分に還元状態と一緒に局所状態をどのように使用するのですか?
import React, {Component} from 'react'
import TableRow from './TableRow'
class Table extends Component {
constructor (props) {
super(props)
this.state = { sortBy: "fistName" }
}
sortContacts (parameter) {
console.log('in sortContacts')
this.setState({ sortBy: parameter })
}
sortedContacts() {
console.log('in sortedContacts')
const param = this.state.sortBy
return (
this.props.data.contacts.sort(function (a, b){
if (!a.hasOwnProperty(param)){
a[param] = " ";
}
if (!b.hasOwnProperty(param)){
b[param] = " ";
}
const nameA = a[param].toLowerCase(), nameB = b[param].toLowerCase();
if (nameA > nameB) {
return 1;
} else {
return -1;
}
})
)
}
filteredSortedContacts() {
console.log('in filteredSortedContacts')
const filterText = this.props.data.filterText.toLowerCase()
let filteredContacts = this.sortedContacts()
if (filterText.length > 0) {
filteredContacts = filteredContacts.filter(function (contact){
return (
contact.hasOwnProperty('lastName') &&
contact.lastName.toLowerCase().includes(filterText)
)
})
}
return filteredContacts
}
contactRows() {
console.log('in contactRows')
return this.filteredSortedContacts().map((contact, idx) =>
<TableRow contact={contact} key={idx}/>
)
}
render() {
return (
<div className="table-container">
<table className="table table-bordered">
<thead>
<tr>
<th className="th-cell" onClick={this.sortContacts.bind(this, "firstName")}>First Name</th>
<th onClick={this.sortContacts.bind(this, "lastName")}>Last Name</th>
<th>Date of Birth</th>
<th>Phone</th>
<th>Email</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
{this.contactRows()}
</tbody>
</table>
</div>
)
}
}
export default Table
をフィルタリング含み、現在のコードの
const Table = React.createClass({
getInitialState() {
return {contacts: []}
},
componentWillReceiveProps() {
this.setState({ contacts: this.props.data.contacts})
},
sortContacts (parameter, e){
...
},
render() {
return (
<table>
<thead>
<tr>
<th onClick={this.sortContacts.bind(this, "firstName")}>First Name</th>
</tr>
</thead>
<tbody>
{contactRows}
</tbody>
</table>
)
}
})
更新。私は体内で一度だけと呼んでいるなら、どうしてこのようなことが起こるのか分かりません。
'sortContants'のレデューサーと内容を表示します。 – luboskrnac