在我的react/redux应用程序中单击按钮时发出一个简单的get请求
Making a simple get request when clicking a button in my react/redux application
对不起,如果这个问题太广泛,我似乎找不到一个适合我正在做的事情或有意义的例子。
基本上,我的一个reducer是一个对象,它包含列表中每个按钮拥有的id,名称和url。每当我单击其中一个按钮时,我想对该url发出get请求,并以列表的形式返回它包含的任何数据。目前,应用程序所做的只是打印出我按下的按钮的id、名称和url。我该如何向url发出get请求并显示包含的内容呢?
下面是我当前的代码:
//example reducer of the button:
export default function(){
return [
{
id: 0,
name: button1,
url: http://jsoncodeINeed.com/api
},
{
id: 1,
name: button2,
url: http://otherJsoncodeINeed.com/api
}
];
}
//reducer that will take care of the action:
export default function(state=null, action){
switch(action.type){
case 'MODULE_CLICKED':
return action.payload;
break;
}
return state;
}
//actions
export const moduleClicked = (module) =>{
return {
type: "MODULE_CLICKED",
payload: module
}
}
//container for where I want to post the data in the button:
class ModuleLog extends Component{
render(){
return (
<Panel
{this.props.module.name}{this.props.module.url}
</Panel>
);
}
function mapStateToProps(state){
return {
module: state.moduleLog
};
}
export default connect(mapStateToProps)(ModuleLog);
//container for the button group:
class ModuleButtons extends Component{
createListItems(){
return this.props.modules.map((module)=>{
return (
<ListGroupItemkey ={module.id}
onClick={()=>this.props.moduleClicked(module)}>{module.name}</ListGroupItem>
);
});
}
render(){
return (
<ListGroup>
{this.createListItems()}
</ListGroup>
);
}
}
function mapStateToProps(state){
return {
modules: state.modules
};
}
function matchDispatchToProps(dispatch){
return bindActionCreators({moduleClicked: moduleClicked}, dispatch)
}
export default connect(mapStateToProps, matchDispatchToProps)(ModuleButtons);
我的存储在index.js文件中,我在其中简单地显示组件,这些组件简单地显示容器。
您应该阅读redux页面中的AsyncActions文档。从本质上讲,您希望从单击按钮时触发的操作发出请求。接收到响应后,使用接收到的数据触发第二个操作(类似于"SUCCESS_REQUEST")。然后,reducer将相应地更新存储,这将更新您的UI。
相关文章:
- Moment/Jquery-一个简单时间线的愚蠢问题
- 问题用moment JS制作一个简单的时间表
- 测试一个简单的javascript按钮
- 用Jquery map和moment js制作一个简单的时间线
- PayPal按钮是否有一个简单的选项,其中金额基于一些用户输入
- 调试一个简单的jQuery函数;想知道是否与其他代码冲突
- 一个简单的粘性头jQuery脚本的问题
- Sencha Touch使一个简单的按钮工作
- html表单,它有文本和表,现在我想在一个简单的文本文件中保存和检索数据,如何实现它
- 如何在Javascript中定义一个简单的自动重启倒计时
- 如何使一个简单的HTML文件成为wordpress中可用的页面
- 如何用D3添加一个简单的圆弧
- angularjs做一个简单的倒计时
- 创建一个简单的处理 p5.js 库
- 是否有一个简单的 JQuery 过程来替换页面上每段内部文本的字符串
- 如何在构造函数中创建一个简单的DOM元素,并在调用时将其附加到元素中
- 是否可以将PHP的一个简单片段重新编码为javascript
- 尝试创建一个简单的求和计算器
- 如何使用jquery添加一个简单的转换
- 正在寻找一个简单的JavaScriptHTMLGUI构建器,我可以将其嵌入到我的网站中