我必须在Redux-React上进行申请。但是不幸的是我出错了:这些是主要的项目文件:<
看起来您的帖子大部分是代码;请添加更多详细信息。看来您的帖子大部分是代码;请添加更多详细信息。看来您的帖子大部分是代码;请添加更多详细信息。
这些是主要的项目文件:
app.js(component):
import React from 'react';
import { Component } from 'react';
import { connect } from 'react-redux'
import { fetchData } from '../actions';
import TableData from "./TableData";
import TableSearch from "./TableSearch";
export function searchFilter (search, data) {
return data.filter(n => n.term.toLowerCase().includes(search));
}
const days = ["23-08-2019", "24-08-2019", "25-08-2019"];
class Root extends React.Component {
componentDidMount() {
this.props.onFetchData(this.props.day);
}
render() {
const { search, shift, data, filteredData, onFilter, onSetSearch, onFetchData } = this.props;
return (
<div>
<TableSearch value={search}
onChange={(e) => onSetSearch(e.target.value)}
onSearch={() => onFilter()} />
{days.map((day, i) => (
<button key={day}
onClick={() => onFetchData(day)}
className={i === day ? "active" : ""}>{day}</button>
))}
<br />
{Object.keys(data).map(n => (
<button data-shift={n}
onClick={(e) => onFilter({ shift: e.target.dataset.shift })}
className={n === shift ? "active" : ""}>{n} shift</button>
))}
<TableData data={filteredData} />
</div>
);
}
}
export const ConnectedRoot = connect(
(state) => state,
(dispatch) => ({
onFilter: (args) => dispatch({ type: 'RUN_FILTER', ...args }),
onSetSearch: (search) => dispatch({ type: 'SET_SEARCH', search }),
onFetchData: (day) => dispatch(fetchData(day))
})
);
答案 0 :(得分:1)
您在初始渲染时得到的数据不确定,请先检查数据是否可用,然后再映射数据:
data && Object.keys(data).map()
根据您的请求,您应该在app.js中使用return语句,例如:
return (
<div>
<TableSearch value={search}
onChange={(e) => onSetSearch(e.target.value)}
onSearch={() => onFilter()} />
{days && days.map((day, i) => (
<button key={day}
onClick={() => onFetchData(day)}
className={i === day ? "active" : ""}>{day}</button>
))}
<br />
{data && Object.keys(data).map(n => (
<button data-shift={n}
onClick={(e) => onFilter({ shift: e.target.dataset.shift })}
className={n === shift ? "active" : ""}>{n} shift</button>
))}
{data && <TableData data={filteredData} /> }
</div>
);