Javascript 切片工作不正常|反应
在我的构造函数中,我有:Javascript 切片工作不正常|反应,javascript,reactjs,splice,Javascript,Reactjs,Splice,在我的构造函数中,我有: constructor(props) { super(props) this.state = { showCount: 4, event: [], customer: [], lock: [] } } componentDidMount() { const { data } = this.props axios.get('http://34.248.65.20:3000/customers').then(res =&
constructor(props) {
super(props)
this.state = {
showCount: 4,
event: [],
customer: [],
lock: []
}
}
componentDidMount() {
const { data } = this.props
axios.get('http://34.248.65.20:3000/customers').then(res => {
this.setState({
res,
data: res.data
})
})
axios.get('http://34.248.65.20:3000/events').then(res => {
this.setState({
res,
data: res.data
})
})
axios.get('http://34.248.65.20:3000/locks').then(res => {
this.setState({
res,
data: res.data
})
})
}
const { event, customer, lock, data } = this.state
const keyedCustomers = _.keyBy(customer, '_id')
const keyedLocks = _.keyBy(lock, '_id')
const events = event
.slice(0, this.state.showCount)
.map((event, i) =>
<EventItem
key={i}
event={event}
customer={keyedCustomers[event.customer] || {}}
lock={keyedLocks[event.lock] || {}}
/>
)
console.log(events) // A lot of object
在我的componentDidMount
中,我有以下内容:
constructor(props) {
super(props)
this.state = {
showCount: 4,
event: [],
customer: [],
lock: []
}
}
componentDidMount() {
const { data } = this.props
axios.get('http://34.248.65.20:3000/customers').then(res => {
this.setState({
res,
data: res.data
})
})
axios.get('http://34.248.65.20:3000/events').then(res => {
this.setState({
res,
data: res.data
})
})
axios.get('http://34.248.65.20:3000/locks').then(res => {
this.setState({
res,
data: res.data
})
})
}
const { event, customer, lock, data } = this.state
const keyedCustomers = _.keyBy(customer, '_id')
const keyedLocks = _.keyBy(lock, '_id')
const events = event
.slice(0, this.state.showCount)
.map((event, i) =>
<EventItem
key={i}
event={event}
customer={keyedCustomers[event.customer] || {}}
lock={keyedLocks[event.lock] || {}}
/>
)
console.log(events) // A lot of object
在我的渲染中,我有这样一个:
constructor(props) {
super(props)
this.state = {
showCount: 4,
event: [],
customer: [],
lock: []
}
}
componentDidMount() {
const { data } = this.props
axios.get('http://34.248.65.20:3000/customers').then(res => {
this.setState({
res,
data: res.data
})
})
axios.get('http://34.248.65.20:3000/events').then(res => {
this.setState({
res,
data: res.data
})
})
axios.get('http://34.248.65.20:3000/locks').then(res => {
this.setState({
res,
data: res.data
})
})
}
const { event, customer, lock, data } = this.state
const keyedCustomers = _.keyBy(customer, '_id')
const keyedLocks = _.keyBy(lock, '_id')
const events = event
.slice(0, this.state.showCount)
.map((event, i) =>
<EventItem
key={i}
event={event}
customer={keyedCustomers[event.customer] || {}}
lock={keyedLocks[event.lock] || {}}
/>
)
console.log(events) // A lot of object
const{event,customer,lock,data}=this.state
const keyedCustomers=u.keyBy(客户,_id'))
const keyedLocks=u.keyBy(锁、_id'))
常量事件=事件
.slice(0,此.state.showCount)
.map((事件,i)=>
)
console.log(events)//很多对象
下面是对象事件:
但是我没有在我的应用程序中看到我的任何事件(一个事件基本上是一个li
)。我认为这将默认显示我的四个事件(showCount:4
)。因为我想有一个按钮,稍后将显示所有这些
有人能告诉我我做错了什么吗
干杯 如果您的res
与{event:[]}
不同,您应该执行以下操作:
axios.get('http://34.248.65.20:3000/events').then(res => {
this.setState({
...
event: res.data
})
})
如果您的res
与{event:[]}
不同,您应该执行以下操作:
axios.get('http://34.248.65.20:3000/events').then(res => {
this.setState({
...
event: res.data
})
})
请添加更多代码来说明此问题。渲染函数中的事件是什么?你的意思是this.state.event
?你在控制台中看到错误了吗?@DanielAndrei已经添加了更多的代码。@MaximKuzmin Nopp,没有错误:/@MartinNordström你需要发布“event”对象的示例(你可以通过调试器或console.log检查),请添加更多代码来说明这个问题。渲染函数中的事件是什么?你的意思是this.state.event
?你在控制台中看到错误了吗?@DanielAndrei已经添加了更多的代码。@MaximKuzmin Nopp,没有错误:/@MartinNordström你需要发布“event”对象的示例(你可以通过调试器或console.log检查它)