【发布时间】:2020-04-06 18:02:18
【问题描述】:
我正在使用 font-picker-react 包来使用 Google Font API 呈现字体。
每次从下拉列表中选择新字体时,我都想用它来更新字段值。
目前,在 useEffect 挂钩中,“值”会正确更新。但是,当控制台在组件主体中记录“值”时,这不会更新,我不确定为什么。
export function FontPickerInputField<T = any>({ field }: FontPickerSidebarProps<T>) {
const placeholderFont: string = 'Open Sans';
const [fontFamily, setFontFamily] = useState(placeholderFont);
let { value, name } = field;
const fontFormat: string = fontFamily.replace(/\s/g, '+');
const updatedFont = `https://fonts.googleapis.com/css?family=${fontFormat}:300,300i,400,400i,500,500i,700,700i,900,900i&display=swap`;
const [googleFontLink, setGoogleFontLink] = useState(updatedFont);
useEffect(() => {
setGoogleFontLink(updatedFont);
value = googleFontLink;
}, [fontFamily]);
return (
<StyledContainer>
<FontPicker
apiKey="MY-API-KEY"
activeFontFamily={fontFamily}
onChange={({ family }) => setFontFamily(family)}
limit={100}
sort={'popularity'}
/>
</StyledContainer>
);
}
【问题讨论】:
-
field是从父级传递的道具,因此无法从子级更新其value。你能分享一下这个field道具是从哪里来的吗?
标签: javascript reactjs typescript react-hooks