[英]Display radio buttons depending on a constant
我想顯示一些等於 const total_regions 的單選按鈕。 我想給每個人一個不同的價值。
例如,如果 total_regions = 5,則顯示五個單選按鈕,其值為 1 - 5。
import React from 'react';
const Test = props => {
const total_regions = (JSON.parse(JSON.stringify(props.test)).length); // gets the number of regions
return (
<ul>
{props.test.map(item => {
return <li>{item.length}</li>;
})}
</ul>
);
};
export default Test;
我玩弄道具試圖到達某個地方,但我不知道如何為每個條目打印具有特定值的每個按鈕。 我可以把 total_regions 變成一個數組並使用它的映射嗎
應用程序.js:
import "bootstrap/dist/css/bootstrap.css";
import React from "react";
import ReactPlayer from 'react-player'
import LeftPane from "./components/LeftPane.js";
import Video from "./components/Video.js";
import Footer from "./components/Footer.js";
import Test from "./components/Test.js";
//import './App.css';
class App extends React.Component {
constructor(props) {
super(props);
this.state = { apiResponse: [] };
}
// Comunicate with API
callAPI() {
fetch("http://localhost:9000/IntensityAPI") //React app talks to API at this url
.then(res => res.json())
.then(res => this.setState({ apiResponse: res }));
}
componentWillMount() {
this.callAPI();
}
render() {
return (
<div className="App">
<div class="row fixed-top fixed-bottom no-gutters" >
<div class="col-3 fixed-top fixed-bottom">
<LeftPane></LeftPane>
</div>
<div class="offset-md-3 fixed-top fixed-bottom" >
<Video></Video>
</div>
<div class=" col-3 fixed-bottom">
// <Footer test = {this.state.apiResponse}/>
<Test test = {this.state.apiResponse}/>
</div>
</div>
</div>
);
}
}
export default App;
您可以使用數組的index
參數::map function 和 map 到無線電類型輸入
{props.test.map((item, index) => {
return (
<label key={index}>
{item}
<input type="radio" value={index} />
</label>
);
})}
您只需要遍歷數據並為數據中的每個項目返回一個單選按鈕,我已經制作了一個示例沙箱供您查看。 希望能幫助到你:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.