我在组件中使用多个钩子,并且需要将钩子传递给值。React 文档有点令人困惑,我不太明白如何格式化该信息以便我的其他组件可以使用它。如何将钩子传递给该值,以便其格式正确,以便可以使用。因为我认为我做错了有人能指出我正确的方向吗?
上下文API
import React, { useState, createContext } from 'react';
export const OptionsContext = createContext();
export const OptionsProvider = props => {
const [name, setName] = useState('');
const [price, setPrice] = useState(0);
const [amountOfOptions, setAmountOfOptions] = useState(0);
const [totalAmountSpent, setTotalAmountSpent] = useState(0);
const [listOfOptions, setListOfOptions] = useState([]);
const { clock } = new Date().toLocaleDateString();
return (
<OptionsContext.Provider value={
[name, setName],
[price, setPrice],
[amountOfOptions, setAmountOfOptions],
[totalAmountSpent, setTotalAmountSpent],
[listOfOptions, setListOfOptions],
clock
}>
{ props.children}
</OptionsContext.Provider>
);
}
Passing the API here …Run Code Online (Sandbox Code Playgroud)