我学习redux并做出反应。我正在学习一些教程,以便制作应用程序。
我有action
:
export function getDueDates(){
return {
type: 'getDueDate',
todo
}
}
这是商店:
import { createStore } from 'redux';
import duedates from './reducers/duedates'
export default createStore(duedates)
这是减速器:
导入不可变的' immutable'
export default (state = Immutable.List(['Code More!']), action) => {
switch(action.type) {
case 'getDueDate':
return state.unshift(action.todo)
default:
return state
}
}
在entry point js
我有这个:
import React from 'react';
import ReactDOM from 'react-dom';
import store from './app/store'
import { Provider } from 'react-redux'
import App from './app/Components/AppComponent';
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('app')
);
现在,(根据一些示例),我应该从getDueDate
调用dispatch
,但我不知道如何获取组件上的dispatch
,以触发操作
答案 0 :(得分:5)
使用tinyint(1)
包中的#include "stm32f4x.h"
#define LED_BLUE_GPIO GPIOC
#define LED_BLUE_PIN 8
void Delay_ms(uint16_t ms){
TIM3->ARR = ms; //timer 3
TIM3->CNT = 0;
while((TIM3->SR & TIM_SR_UIF)==0){}
TIM3->SR &= ~TIM_SR_UIF;
}
int main(void)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPCEN;
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
//initialization
TIM3->PSC = 23999; // f timer = fclk / 24000 => 1kHz
TIM3->ARR = 0xFFFF;
TIM3->CR1 = TIM_CR1_CEN;
DBGMCU->CR = DBGMCU_CR_DBG_TIM3_STOP;
while(1){
GPIOC->ODR = GPIO_ODR_ODR8;
Delay_ms(1000); //delay 1 sec
GPIOC->ODR = GPIO_ODR_ODR8;
Delay_ms(1000); //delay 1 sec
}
}
。它有两个函数,如params,connect
和react-redux
,您现在感兴趣。根据Nick Ball的回答,这部分是正确的,你会像这样出口:
mapStateToProps
并且您的mapDispatchToProps
看起来像这样:
export default connect(mapStateToProps, mapDispatchToProps)(App)
只要连接到商店的组件具有从上方传递的属性ID,您就可以从内部调用mapDispatchToProps
。
编辑:在这种情况下可能不需要使用function mapDispatchToProps (dispatch, ownProps) {
return {
getDueDate: dispatch(getDueDate(ownProps.id))
}
}
,但我的观点是指出道具作为第二个参数:)
答案 1 :(得分:2)
这里缺少的部分是来自react-redux
的{{3}}函数。此功能将组件“连接”到商店,为其提供dispatch
方法。关于如何做到这一点有不同的变化,所以我建议阅读文档,但一个简单的方法是这样的:
// app/Components/AppComponent.js
import { connect } from 'react-redux';
export class App extends React.Component {
/* ...you regular class stuff */
render() {
// todos are available as props here from the `mapStateToProps`
const { todos, dispatch } = this.props;
return <div> /* ... */ </div>;
}
}
function mapStateToProps(state) {
return {
todos: state.todos
};
}
// The default export is now the "connected" component
// You'll be provided the dispatch method as a prop
export default connect(mapStateToProps)(App);