【问题标题】:Getting error on the lift state up between the 2 react components need & tried various method passing between the 2 components在 2 个反应组件之间的提升状态出现错误需要并尝试在 2 个组件之间传递的各种方法
【发布时间】:2021-05-05 08:24:45
【问题描述】:

尝试以各种方法和方式解决问题,但没有解决。我不知道为什么会出现错误我不明白代码中的确切问题。任何人都可以帮助解决问题吗?

参考:https://codesandbox.io/s/gifted-gagarin-k54vf

<TextField
              id="productTitle"
              name="title"
              label="Product Title"
              ariaLabel="Product Title"
              value={values.title}
              onChange={handleChange}
              onBlur={handleBlur}
              error={!!errors.title}
              helperText={errors.title ? errors.title : ""}
              showLabelHelperText={false}
              labelHidden={true}
              inputProps={{ maxLength: 60 }}
            />


    
    
    
              <Grid item xs={12} sm={10}>
                <Typography color="textSecondary" className={classes.newproducts}>
                {values.title || "New Product"}
                </Typography>
              </Grid>

嗨,如何在这 2 个组件之间进行反应提升状态,第一个文本字段是一个组件(Form.js),而 Grid 部分在另一个组件文件(app.js)中 如果我尝试更改它需要更新的文本字段,用输入的文本字段替换新产品

你可以使用这种方法作为代码和框https://codesandbox.io/s/lifting-state-up-example-1-hooks-forked-ygfbh的参考

【问题讨论】:

  • 您能否说明您在何处以及如何定义values 变量?
  • @Nico Graf 检查密码框codesandbox.io/s/gifted-gagarin-k54vf
  • 请在您的问题中显示minimal reproducible example。您也可以包含指向代码沙箱的链接,但您的问题应该显示足够的代码,以便我们了解您正在尝试做什么。
  • 另外,edit你的问题告诉我们是哪一行导致了错误。
  • @prohith 我在代码沙箱中的问题中找不到您的代码。此外,代码无法编译。显示另一个错误!请减少代码,这样我们就不必自己搜索并使其编译没有错误。然后告诉我们究竟是哪个部分有问题。

标签: javascript node.js reactjs react-redux


【解决方案1】:

你的代码有问题

  1. 缺失值
  2. &lt;Router/&gt;之外使用&lt;Link/&gt;

修复 1:当您不确定时使用简单的三元运算符

/src/App.js:61

{values &amp;&amp; values.title ? values.title : "New Product"}

修复 2:

切勿使用 &lt;Link/&gt;&lt;Router&gt;

更新示例:

https://codesandbox.io/s/romantic-pine-qym61?file=/src/App.js:1599-1654

【讨论】:

猜你喜欢
  • 2021-04-30
  • 2017-10-24
  • 2018-02-22
  • 1970-01-01
  • 2016-07-20
  • 2019-07-24
  • 1970-01-01
  • 1970-01-01
  • 2019-07-18
相关资源
最近更新 更多