求大神 带带
Js 如何去修改 react组件的内容
huangff1986
#4
React 设计思想是不允许js直接操作react组件中内容的。
组件中可变的地方都存放在this.state。组件内部通过其他方式从外部获取数据,然后由组件自身去修改this.state。达到修改内容的方式。
所以换个思路想想吧。
wuyafeiJS
#7
在组件生命周期componeDidMount 里面是是可以用jquery来操作DOM的。但react不推荐这么做,react实现页面更新一般就两种方式:改变state,或者改变props。建议学习react基础。我之前看的是阮一峰的react技术栈,希望对你有用
lobos
#8
两个方案,一个是cloneElement
import { cloneElement } from 'react'
function MyComponent({children, ...props}) {
const otherProps = {...}
return cloneElement(children, {...props, ...otherPops })
}
<MyComponent><OtherComponent {...} /></MyComponent>
另一个方案,高阶组件
function wrap(OldComponent) {
return function NewComponent(props) {
const otherProps = {...}
return <OldComponent {...props} {...otherProps} />
}
}
......
class MyComponent extends Component { ... }
export default wrap(MyComponent)
1 Like