죄송합니다.이 질문이 너무 광범위하다면, 나는 옳은 일이나 내가하고있는 일에 합당한 예를 찾을 수없는 것 같습니다.react/redux 응용 프로그램에서 버튼을 클릭하면 간단한 요청을 내게됩니다.
기본적으로 내 감속기 중 하나는 목록에있는 각 버튼의 ID, 이름 및 URL이 포함 된 개체입니다. 단추 중 하나를 클릭 할 때마다 해당 URL에 요청을 보내고 목록 형식으로 포함 된 모든 데이터를 반환하고 싶습니다. 현재 모든 응용 프로그램은 내가 누른 버튼의 ID, 이름 및 URL을 인쇄합니다. 어떻게 세상에서 URL에 대한 요청을하고 무엇이 들어 있는지 표시 할 수 있습니까?
//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-thunk' 미들웨어를 사용할 수 있습니다. –
작업에서 실제 비동기 호출을 수행합니다. 썽크를 사용하거나 mapDispatchToProps를 사용할 수 있습니다 (그러면 다른 라이브러리가 필요하지 않습니다). 기본 아이디어는 행동 창작자에게 전화하는 데 필요한 모든 정보를 전달하고 전화를 걸고 결과 데이터를 감속기에 전달하여 주에 배치하는 것입니다. 또한 하나의 빠른 메모 - 귀하의 감속기에서 당신은 현재 상태에서 무엇이든지 무시할 것입니다 국가 대신 action.payload를 반환합니다, 그래서 조심하세요. – ajmajmajma
@ajmajmajma 나는 수동으로 집어 넣은 URL을 사용하여이 작업을 수행하려하지만 현재 해당 작업을 나머지 응용 프로그램과 연결하는 데 어려움을 겪고 있습니다. 다른 URL에있는 URL에서 어떻게 처리할지 모를 것입니다. 감속기. – theJuls