Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript属性在React中用作默认状态时显示为未定义_Javascript_Reactjs_Redux_React Redux_React Hooks - Fatal编程技术网

Javascript属性在React中用作默认状态时显示为未定义

Javascript属性在React中用作默认状态时显示为未定义,javascript,reactjs,redux,react-redux,react-hooks,Javascript,Reactjs,Redux,React Redux,React Hooks,我通过useffect获取了一些内容,作为应用程序的默认状态。请参阅下面的代码: const id = props.match.params.id; const classes = UseStyles(); const dispatch = useDispatch(); useEffect(() => dispatch(fetchCategories()), []); const categories = useSelector((state) => state).generalR

我通过
useffect
获取了一些内容,作为应用程序的默认状态。请参阅下面的代码:

const id = props.match.params.id;
const classes = UseStyles();
const dispatch = useDispatch();

useEffect(() => dispatch(fetchCategories()), []);
const categories = useSelector((state) => state).generalReducer.category;

useEffect(() => dispatch(fetchBusinessDetail(id)), []);
const business = useSelector((state) => state).generalReducer.businessDetail;
console.log(business);

console.log(business.name);
let defaultState = {
  name: business.name,
  description: business.description,
  url: business.url,
  phone: business.phone,
  address: business.address,
  category_id: [],
  email: business.email,
  image: "",
};

const [formState, setFormState] = useState(defaultState);
console.log(formState);
console.log(business)
console.log(business.name)
给出了预期的结果,
console.log(formState)
给出了如下未定义的值:

{
  // ...
  image: "";
  name: undefined;
  phone: undefined;
  url: undefined;
}
如何修复此问题?

使用,
initialState
将是初始渲染上的值

在初始呈现期间,返回的状态(state)与作为第一个参数(initialState)传递的值相同

由于您获取
defaultState
,因此它的初始值是您从redux存储中获取的值:

// business values are values from redux store
let defaultState = {name: business.name, description: business.description, url:business.url, phone:business.phone, address: business.address, category_id: [], email:business.email, image:''  };
通常,在完成抓取后更新状态

const businessSelector = state => state.generalReducer.businessDetail;

const business = useSelector(businessSelector);

// Fetch and update `business` above.
useEffect(() => dispatch(fetchBusinessDetail(id)), []);

// `business` get new values and update the form.
useEffect(() => {
  setFormState(business);
}, [business]);

是的,在获取数据后,他必须使用
setFormState
进行设置,你是对的,你能解释一下
useSelector((state)=>state)的用法吗。generalReducer.businessDetail此代码的作用是什么?输入操作问题,它应该是一个选择器