微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

React 组件的 defaultValue 不起作用

如何解决React 组件的 defaultValue 不起作用

我正在尝试使用从数据库获取的值作为输入字段的认值(显示直到单击组件)。

但是,使用“defaultValue”属性,它不会显示获取的值。

App.tsx 文件(取值成功):

  const [Income,setIncome] = useState<string>('0');

    useEffect(() => {

    const incomeResp = async () => {
      await axios.get('http://localhost:4000/app/income')
      .then(
        result => setIncome(result.data && result.data.length > 0 ? result.data[0].income : 0))
    }
    incomeResp();
  },[]);


  return (
      <div className="App">
        <div>
          <IncomeExpensesContainer 
            income={Income}
            setIncome={setIncome} 
            totalExpenses={TotalExpensesAmount} 
            currencySymbol={Currency}
          />
      </div>

组件文件

interface Props {
    income: string;
    setIncome: (value: string) => void; 
    totalExpenses: number;
    currencySymbol: string;
}

const IncomeExpensesContainer: React.FC<Props> = ({
        income,setIncome,totalExpenses,currencySymbol,}: Props) => {


   const [insertedValue,setInsertedValue] = useState<string>(income);

return (
    <Grid container spacing={1} className="income-expenses-container">
        <InputItem 
            onChange={setInsertedValue}
            onBlur={setIncome}
            title="Income" 
            type="number" 
            placeholder="Your income" 
            defaultValue={income}
        />
    </Grid>
);

在这里遗漏了什么?

解决方法

在提供其他信息后进行编辑

在你的组件中使用传递的 prop:

error[E0583]: file not found for module `config`
 --> src/lib.rs:7:1
  |
7 | mod config;
  | ^^^^^^^^^^^
  |
  = help: to create the module `config`,create file "src/lib/config.rs"

error[E0433]: failed to resolve: could not find `Config` in `config`
  --> src/lib.rs:16:26
   |
16 |     let config = config::Config::new(args).unwrap_or_else(|err| {
   |                          ^^^^^^ could not find `Config` in `config`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0433,E0583.
For more information about an error,try `rustc --explain E0433`.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。