Я пытаюсь получить значения из компонента формы, используя реквизиты в основном компоненте приложения, где находится мое состояние. Используя значения, которые я получаю от реквизита, я хочу обновить основное состояние. Это мой код:
класс Приложение расширяет компонент {state = {dataset: "", cc: "", cw: 0, ch: 0, bw: 0, bh: 0, xspacing: 0 , yspacing: 0, barcolor: "",};
// Function to use the data got from form component and setState
dataForm = (
dataset,
canvascolor,
canvaswidth,
canvasheight,
barwidth,
barheight,
xspacing,
yspacing,
barcolor
) => {
this.setState({
dataset,
canvascolor,
canvaswidth,
canvasheight,
barwidth,
barheight,
xspacing,
yspacing,
barcolor,
});
console.log(this.state);
};
render() {
return (
<div className="App">
{/* This is the Form Component from where i'm getting my Data */}
<DataForm dataForm={this.dataForm} />
<DataGraph graph={this.state} />
</div>
);
}
}
Я пытаюсь отправить свое обновленное состояние компоненту DataGraph, используя реквизиты, но это не работает. Это код моего компонента DataGraph ниже:
class DataGraph extends Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
}
drawChart() {
const d = this.props.graph.dataset.split`,`.map((x) => +x);
const accessToRef = d3
.select(this.myRef.current)
.append("svg")
.attr("width", this.props.graph.cw)
.attr("height", this.props.graph.ch)
.style("background-color", this.props.graph.cc);
accessToRef
.selectAll("rect")
.data(d)
.enter()
.append("rect")
.attr("x", (d, i) => i * this.props.graph.xspacing)
.attr("y", (d, i) => this.props.graph.ch - this.props.graph.yspacing * d)
.attr("width", this.props.graph.bw)
.attr("height", (d, i) => d * this.props.graph.bh)
.attr("fill", this.props.graph.barcolor);
}
componentDidMount() {
this.drawChart();
}
render() {
return (
<>
<div
style={{
display: "flex",
justifyContent: "center",
alignItems: "center",
}}
ref={this.myRef}
className="pb-5"
></div>
</>
);
}
}
DataGraph.propTypes = {
graph: PropTypes.object.isRequired,
};
setState()
не устанавливает значения состояния. Пожалуйста, помогите.