reactjs - ReactNative 错误如果您打算呈现子集合,则对象作为反应子无效,请改用数组
问题描述
所以我有一个对象。例如 {'monday': 1, 'tuesday': 2} 我正在使用 props.getProductiveDay() 在用户登录当天获取更新的对象并且我想在水平条形图中呈现这些数据data 属性,但我得到了错误 Objects are not valid as react child use an array instead
const HorizontalBarGraphComponent = (props) => {
useEffect(() => {
props.getProductiveDay()
}, [])
return (
<HorizontalBarGraph
data={[2, 3, 1, 2, 1, 2, 3]}
labels={["Sat", "Fri", "Thurs", "Wed", "Tues", "Mon", "Sun"]}
width={Dimensions.get("window").width - 70}
height={220}
barRadius={2}
barWidthPercentage={0.4}
barColor="rgb(105, 126, 240)"
baseConfig={{
hasYAxisBackgroundLines: false,
xAxisLabelStyle: {
rotation: 0,
fontSize: 13,
width: 60,
yOffset: 4,
xOffset: -12,
},
hasYAxisLabels: false,
}}
style={{
// marginLeft: 20,
marginRight: 60,
marginTop: -60,
padding: 10,
// borderWidth: 1,
// borderColor: "#EEEEEE",
borderRadius: 10,
width: Dimensions.get("window").width - 150,
// backgroundColor: `#FAFAFA`,
}}
/>
)
}
const mapStateToProps = (state) => {
return {
productiveDay: state.productiveDayReducer,
}
}
const mapDispatchToProps = (dispatch) => {
return {
getProductiveDay: () => dispatch(getProductiveDay()),
}
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(HorizontalBarGraphComponent)
解决方案
这是上述问题的减速器的动作广告
export const getProductiveDay = () => {
return async (dispatch) => {
const data = await AsyncStorage.getItem("productiveDay")
const parse = JSON.parse(data)
console.log("parse", parse)
return dispatch({
type: "FETCH_OVERALL_DISCIPLINE_LEVELS",
data: parse,
})
}
}
const daysOfTheWeek = [
{
Monday: 1,
Tuesday: 1,
Wednesday: 1,
Thursday: 1,
Friday: 1,
Saturday: 1,
Sunday: 2,
},
]
const productiveData = async (state) => {
try {
let json = JSON.stringify(state)
await AsyncStorage.setItem("productiveDay", json)
} catch (err) {
console.log(err)
}
}
const productiveDayReducer = (state = daysOfTheWeek, action) => {
switch (action.type) {
case "FETCH_OVERALL_DISCIPLINE_LEVELS":
return action.data
case "INCREASE_PRODUCTIVITY":
// state[day] += 1
productiveData(state)
return state
case "DECREASE_PRODUCTIVITY":
// state[day] -= 1
productiveData(state)
return state
default:
return state
}
}
export default productiveDayReducer
推荐阅读
- django - 如何使用修改后的 listField 序列化 DRF 上的更新
- umbraco7 - 数据未加载到 Jqwidget DataTable (JqDataTable) 中
- android - 以编程方式从Android中的自定义默认拨号器呼叫活动/应答时拨打数字(1或2)?
- openstreetmap - 一个月前在 OSM 中的位置如何?
- mysql - 有什么方法可以将独立于 Mysql 版本的 Mysql 服务器连接到 vb.net 应用程序?
- node.js - 在 Postman nodejs 中使用不记名授权
- java - 可选字段不存在与休息请求正文中存在空值的字段之间的区别
- xml - 仅选择 XSLT 中的数字
- c++ - 如何使用 objcopy 将 ARM_ATTRIBUTES 部分添加到二进制文件中
- ios - 方向锁在 xamarin ios 中不起作用