假设我有以下文件。
import React, { useState } from 'react';
import someComputation from './someComputation';
type ComponentType = {
input: number;
};
const Component = (props: ComponentType) => {
const [state, setState] = useState(someComputation(props.input));
console.log(state.color); // "#440000" for example
return <>...some component UI</>;
};
export default Component;
import Component from "./Component"
# Title
## Header
Body text like this
<Component input={5} />
I want to refer to the {state.color} of the component above. How?
我认为一种方法可以是使用状态值更新全局或上下文,然后创建另一个仅获取上下文值的组件。
有没有更好(更容易)的方法来实现这一目标?