I am trying to move keys and values in propsValue to Users using immer.
Furthermore, there are 2 buttons.
"convert them" moves propsValue into a new Array.
"print all" prints all key and value of the new Array.
In my theory, it should be worked perfectly and keys and values in propsValue were moved into
users.
However, it prints "Expected an assignment or function call ..." when the button clicked.
please share your kind advice to solve this problem.
import React from 'react'; import { useImmer } from "use-immer"; const UserImmer = () => { const propsValue = [ { value: "Name", key:"first_name" }, { value: "last Name", key:"last_name" }, { value: "E mail", key:"e_mail" }, { value: "Address", key: "address" } ] const [user, setUser] = useImmer({ name: '', key: '', }) const [users, setUsers] = useImmer([]) const onbuttonChange = () => { //iteration begins propsValue.map(data => ( //moves propsValue key, value into user setUser(draftState => { draftState.name = data.name, draftState.key = data.key }), //push new array key and value into Users setUsers(draftState => { draftState.push(user); }), //initialize the array setUser(draftState => { draftState.name = "", draftState.key = "" }) ) ) } const onClickUser = () => { users.map(temp => console.log(temp.name, " : " , temp.key)); } return ( <div> <button onClick={onClickUser}>print All</button> <button onClick={onbuttonChange}>Convert them</button> </div> ) } export default UserImmer; https://stackoverflow.com/questions/66767568/react-error-expected-an-assignment-or-function-call-and-instead-saw-an-expressi March 24, 2021 at 12:46AM
没有评论:
发表评论