【发布时间】:2021-12-29 14:04:15
【问题描述】:
我是 React 新手。我们可以改变道具的状态吗?例如,我有 2 段代码
App.js
import React, { useState } from 'react'
import Print from '../components/Print/print'
const [text, setText] = useState("Hi");
<Print text = {text} />
print.js
import React, { useState } from 'react'
const Print = (props) => {
return(
<p>props</p>
)
export default Print
有没有办法改变道具的状态,即在print.js 中使用useState() 来更新状态。例如,我们可以在print.js 中做类似setText(prop) 的事情。如果不是这样,那么您将如何触发从 print.js 到 App.js 中的变量 tech 的状态更改?
【问题讨论】:
-
把二传手传下去?传递不同的回调来代理访问它?
-
在 Print 中挂载时,创建一个 state var,将值设置为 prop 值。然后,您可以在不更改道具的情况下更改 print 本身的值。
标签: javascript reactjs react-redux react-hooks