我正在为我的项目做POC。我使用React作为前端。我对reactjs还很陌生。我有一个小问题,有人能帮我吗?
我正在从服务器获取json数据,数据如下:
{
[id: 1,
name: Test data,
option:{ "numeric", "alpha-numeric", "special characters", "language change" ] ....}
}我需要在我的页面上显示单选按钮的选项,以便用户可以选择并转到下一页。在下一页中,我获得了option的另一组数据,这些数据需要显示为单选按钮。我应该如何在reactjs中构建这个通用的单选按钮组件,以便可以跨应用程序中的所有页面使用。
我已经试过这个代码了。但是它将单选按钮打印在一行上。循环不工作
import React, { Component } from "react";
import { PageHeader, ListGroup } from "react-bootstrap";
import "./Home.css";
const url = "http://localhost:xxxx/rest/qas/2";
export default class Question3 extends Component {
constructor(props) {
super(props);
this.state = {
error: null,
isLoaded: false,
selectedOption: false,
QuestionAnswer: [],
counter: 0,
names: ""
};
}
componentDidMount() {
fetch(url)
.then(response => {
console.log(response);
return response.json();
})
.then(json => {
this.setState({
qid: json.qid,
qtext: json.qtext,
point: json.marks,
ans: json.answer,
choices: JSON.stringify(json.options)
});
});
}
// Note: it's important to handle errors here
// instead of a catch() block so that we don't swallow
// exceptions from actual bugs in components.
handleNext = e => {
e.preventDefault();
this.props.history.push("/question4");
};
handlePrevious = e => {
e.preventDefault();
this.props.history.push("/question2");
};
handleOptionChange = e => {
if (e.target.checked) {
console.log(e.target.value);
if (e.target.value === this.state.answer) this.props.counter += 1;
}
};
render() {
const { qid, qtext, point, ans } = this.state;
const data = {
option: [this.state.choices]
};
let radioButtons = [];
const RadioButtons = props => {
const { names } = props;
return (
<div>
{" "}
{names.map(n => (
<Radio name={n} />
))}{" "}
</div>
);
};
const Radio = props => {
const { name } = props;
return (
<div>
<input id={name} type="radio" name="type" value={name} />{" "}
<label for={name}> {name} </label>{" "}
</div>
);
};
//const listItems = replies.map((number) =>
// {number}
//);
return (
<div className="q2">
<div className="labels">
<label for="test">
{" "}
{this.state.qid} {this.state.qtext}{" "}
</label>{" "}
</div>
<div className="App">
<RadioButtons names={data.option} />{" "}
</div>
<div className="Navigation">
<button
type="submit"
className="btn btn-primary"
onClick={this.handleNext}
>
Next{" "}
</button>
{" "}
<button
type="submit"
className="btn btn-primary"
onClick={this.handlePrevious}
>
Previous{" "}
</button>{" "}
</div>{" "}
</div>
);
}
}
发布于 2019-03-08 00:43:20
这是一个非常普遍的问题,但我会尽可能地回答,而不只是给你完整的代码。
您从服务器获得的数据将用于生成RadioButtons,因此您需要将其存储在state/redux/localstorage或w/e中,否则您将选择将其存储在其中。一旦你有了它,你将需要创建一个组件,然后可以通过应用程序重用。有很多关于组件的例子,所以我就不赘述了。在您的组件中,您将需要访问从API存储的数据。对于本例,我将假设您将数据存储在const中,并将其作为一个属性传递给组件:<RadioButtons names={data.options} />
在RadioButtons组件中,您可以使用组件循环渲染中的道具来渲染按钮:
render() {
let radioButtons = [];
this.props.names.map(name => radioButtons.push(<input type="radio" value={name}> {name} <br>));
return <div>{radioButtons}</div>;
}希望这能有所帮助!
发布于 2019-03-08 02:11:06
这里有一个你能做的最小的例子。
RadioButtons是一个组件,它将names作为支撑,然后在其上进行映射以返回Radio组件的列表。Radio组件负责使用name属性并呈现带有标签的单个单选按钮。此外,如果只需要一个选择,请记住保持单选按钮的type不变。
import React from "react";
import ReactDOM from "react-dom";
const data = {
name: "Test data",
option: ["numeric", "alpha-numeric", "special characters", "language change"]
};
const RadioButtons = props => {
const { names } = props;
return (
<div>
{names.map(n => (
<Radio name={n} />
))}
</div>
);
};
const Radio = props => {
const { name } = props;
return (
<div>
<input id={name} type="radio" name="type" value={name} />
<label for={name}>{name}</label>
</div>
);
};
function App() {
return (
<div className="App">
<RadioButtons names={data.option} />
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);希望这能有所帮助。祝你编码愉快!
https://stackoverflow.com/questions/55048052
复制相似问题