在我的项目中,当用户单击“提交”按钮时,我试图记录所有答案。有两个部分,每个部分都有不同的问题。完成第1部分的问题后,当用户单击NEXT按钮时,状态应传递到另一个组件,以便记录我正在使用history.push(/ path,[state])传递数据但无法正常工作的答案。谁能告诉我如何解决这个问题?当我单击“提交”按钮时,只会记录问题4和5的答案,如输出所示。
代码::
Section.js
import React, { Component } from "react";
import { Button } from "semantic-ui-react";
import { withRouter } from "react-router";
import Answers from "../Answers/Answers";
class Section extends Component {
state = {
que1: "",
que2: "",
que3: ""
};
handleClick = event => {
this.setState(
{
que1: event.target.attributes.getNamedItem("data-key").value
},
() => {
console.log(this.state.que1);
}
);
};
handleClick2 = event => {
this.setState(
{
que2: event.target.attributes.getNamedItem("data-key").value
},
() => {
console.log(this.state.que2);
}
);
};
handleClick3 = event => {
this.setState(
{
que3: event.target.attributes.getNamedItem("data-key").value
},
() => {
console.log(this.state.que3);
}
);
};
render() {
let styles = {
width: '50%',
margin: '0 auto',
marginBottom: '15px'
}
console.log(this.state);
const { history } = this.props;
const { que1, que2, que3 } = this.state;
return (
<>
<p>1. I was stressed with my nerves on edge.</p>
<Button.Group selected={this.state.que1} widths="5" onClick={this.handleClick} style={styles}>
<Answers style={{ backgroundColor: 'red' }} />
</Button.Group>
{` `}
<p>2. I lost hope and wanted to give up when something went wrong.</p>
<Button.Group selected={this.state.que2} widths="5" onClick={this.handleClick2} style={styles}>
<Answers style={{ backgroundColor: 'red' }} />
</Button.Group>
{` `}
<p>3. I feel very satisfied with the way I look and act</p>
<Button.Group selected={this.state.que3} widths="5" onClick={this.handleClick3} style={styles}>
<Answers style={{ backgroundColor: 'red' }} />
</Button.Group>
<p />
{` `}
<Button
disabled={!que1 || !que2 || !que3}
onClick={() => history.push("/section2", [this.state])}
>
NEXT
</Button>
</>
);
}
}
export default withRouter(Section);
Section2.js
import React, { Component } from "react";
import { Link } from "react-router-dom";
import { Button } from "semantic-ui-react";
import Answers from "../Answers/Answers";
import CommonButton from "../CommonButton/CommonButton";
import SectionIndicator from "../../components/SectionIndicator/SectionIndicator";
export class Section2 extends Component {
state = {
que4: "",
que5: ""
};
handleClick4 = event => {
this.setState(
{
que4: event.target.attributes.getNamedItem("data-key").value
},
() => {
console.log(this.state.que4);
}
);
};
handleClick5 = event => {
this.setState(
{
que5: event.target.attributes.getNamedItem("data-key").value
},
() => {
console.log(this.state.que5);
}
);
};
handleClickForSubmit = () => {
console.log(this.state)
}
render() {
let styles = {
width: '50%',
margin: '0 auto',
marginBottom: '15px'
}
let style2 = {
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between'
}
console.log(this.state);
const { history } = this.props;
return (
<div style={{ textAlign: 'center' }}>
<SectionIndicator value={"2"} total={"2"} progress={"ratio"} />
<p>4. How many times do you eat in a day when you are stressed.</p>
<Button.Group widths="5" onClick={this.handleClick4} style={styles}>
<Answers />
</Button.Group>
<span />
<p>5. Do you prefer to work when you are stressed.</p>
<Button.Group widths="5" onClick={this.handleClick5} style={styles}>
<Answers />
</Button.Group>
<Link to="/assess" style={style2}>
{" "}
<CommonButton text={"PREVIOUS"} onClick={() => history.goBack()} />
<CommonButton text={"SUBMIT"} onClick={this.handleClickForSubmit} />
</Link>
</div>
);
}
}
export default Section2;
输出::