Error: First argument to `createDraft` must be a plain object, an array, or an immerable object
Error: First argument to `createDraft` must be a plain object, an array, or an immerable object
Home.js
import React, { useState, useEffect } from 'react'
import './HomeCss.css'
import { Card, Button, Container, Row, } from 'react-bootstrap'
import Axios from 'axios'
import { useStoreActions, useStoreState } from 'easy-peasy'
const Layout = () => {
const [items, setItmes] = useState([])
const count = useStoreState(state => state.count)
const add = useStoreActions(actions => actions.add)
useEffect(() => {
Axios.get('http://localhost:3000/products')
.then(res => {
setItmes(res.data)
})
.catch(err => {
console.log(err)
})
})
function displayCard() {
if (!items.length) return null
return items.map((item, index) => (
<Card style={{
width: '21rem',
marginRight: "7px",
marginLeft: '7px',
marginBottom: '13px',
display: 'flex',
alignItems: 'center'
}}>
<Card.Img variant="top" src={item.url} />
<Card.Body>
<Card.Title>{item.title}</Card.Title>
<Card.Text>
{item.description}
</Card.Text>
<Button variant="primary"
onClick={() => add()}
>
Buy Now
</Button>
</Card.Body>
</Card>
))
}
console.log(count)
return (
<div>
<Container><Row>{displayCard()}</Row></Container>
</div>
)
}
export default Layout
由于上面代码中的 onClick={() => add()}
行,我遇到了这个错误:
错误:createDraft 的第一个参数必须是普通对象、数组或可浸入式对象
这就是 model.js
文件的样子,从我在上面的 Home.js
文件中获取 state.count
和 actions.add
的地方看,如果它有用的话:
model.js
import { action } from "easy-peasy";
export default {
count: 0,
add: action((state, id) => {
return state.count + 1
})
};
这里是 link 回购:https://github.com/charanpreet-byte/Commerce-website/blob/master/client/src/pages/Home.js
model.js
import { action } from "easy-peasy";
export default {
count: 0,
add: action((state, id) => {
state.count += 1
})
};
错误来自 Immer。 Immer 允许您使用命令式 API 并且应该像这样修改值 state.count += 1
,而不是在上面的 model.js
文件中返回像这样 return state.count + 1
的新值。
Home.js
import React, { useState, useEffect } from 'react'
import './HomeCss.css'
import { Card, Button, Container, Row, } from 'react-bootstrap'
import Axios from 'axios'
import { useStoreActions, useStoreState } from 'easy-peasy'
const Layout = () => {
const [items, setItmes] = useState([])
const count = useStoreState(state => state.count)
const add = useStoreActions(actions => actions.add)
useEffect(() => {
Axios.get('http://localhost:3000/products')
.then(res => {
setItmes(res.data)
})
.catch(err => {
console.log(err)
})
})
function displayCard() {
if (!items.length) return null
return items.map((item, index) => (
<Card style={{
width: '21rem',
marginRight: "7px",
marginLeft: '7px',
marginBottom: '13px',
display: 'flex',
alignItems: 'center'
}}>
<Card.Img variant="top" src={item.url} />
<Card.Body>
<Card.Title>{item.title}</Card.Title>
<Card.Text>
{item.description}
</Card.Text>
<Button variant="primary"
onClick={() => add()}
>
Buy Now
</Button>
</Card.Body>
</Card>
))
}
console.log(count)
return (
<div>
<Container><Row>{displayCard()}</Row></Container>
</div>
)
}
export default Layout
由于上面代码中的 onClick={() => add()}
行,我遇到了这个错误:
错误:createDraft 的第一个参数必须是普通对象、数组或可浸入式对象
这就是 model.js
文件的样子,从我在上面的 Home.js
文件中获取 state.count
和 actions.add
的地方看,如果它有用的话:
model.js
import { action } from "easy-peasy";
export default {
count: 0,
add: action((state, id) => {
return state.count + 1
})
};
这里是 link 回购:https://github.com/charanpreet-byte/Commerce-website/blob/master/client/src/pages/Home.js
model.js
import { action } from "easy-peasy";
export default {
count: 0,
add: action((state, id) => {
state.count += 1
})
};
错误来自 Immer。 Immer 允许您使用命令式 API 并且应该像这样修改值 state.count += 1
,而不是在上面的 model.js
文件中返回像这样 return state.count + 1
的新值。