gpt4 book ai didi

javascript - 如何将类组件中的 props 发送到功能组件?

转载 作者:行者123 更新时间:2023-12-03 13:44:56 25 4
gpt4 key购买 nike

我是 ReactJS 的初学者。需要知道如何将一个页面中的 props 值发送到另一个页面。 Prop 位于第一页上我可以获取类组件值如何获取另一页中的值。提前致谢

wallcolor.jsx

import React, { Component } from "react";
import { SketchPicker } from 'react-color';
import WallFactory from "../../wall-factory-3d";
export default class WallColor extends Component {

constructor(props) {
super(props);
this.state = {
background: '#d3d3d3',
};
}

handleChangeComplete(e){
this.setState({ background: e.hex },()=>{
return <WallFactory background={this.state.background}/>
});
};

render() {
return (
<SketchPicker color={ this.state.background } onChangeComplete={(e)=>this.handleChangeComplete(e)} />
);
}
}

另一个页面是wall-factory-3d.js

import  { handleChangeComplete } from "../../components/toolbar/wallcolor";
import * as SharedStyle from '../../shared-style';

function get_color(props){
console.log(props.background);
}

我尝试了这个但没有得到输出。

最佳答案

意见,但这是我会做的......:)

this.state = {
background: 'color',
pickedWall: false,
}

const handleChangeComplete = (e) => {
this.setState(prevState => ({
...prevState,
pickedWall: true,
background: e.target.value,
}))
}

render() {
const { x, y } = this.props
const { background, pickedWall } = this.state
const { handleChangeComplete } = this

return <SketchPicker {...{ x, y, background, handleChangeComplete, pickedWall }} />
}

在你的组件中...

const SketchPicker = ({
x,
y,
background,
pickedWall,
handleChangeComplete,
}) => {
return (
<div>
<SomeDiv onClick={(e) => handleChangeComplete(e)}>
...your code
</SomeDiv>
<Fragment>
{pickedWall && <WallPicker {...{ x, y, background }} />}
</Fragment>
</div>
)
}

关于javascript - 如何将类组件中的 props 发送到功能组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60280280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com