我坚持用redux连接器导出material-ui样式.这是我的代码:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import Drawer from 'material-ui/Drawer';
import { withStyles } from 'material-ui/styles';
import PropTypes from 'prop-types';
const mapStateToProps = state => ({});
const reduxConnector = connect(mapStateToProps, null);
const styles = theme => {
console.log(theme);
return ({
paper: {
top: '80px',
boxShadow: theme.shadows[9]
},
});
};
class Cart extends Component {
render() {
const { classes } = this.props;
return (
<Drawer
open
docked
anchor="right"
classes={{ paper: classes.paper }} …Run Code Online (Sandbox Code Playgroud) 反应还原函数的函数mapStateToProps和mapDispatchToProps参数之间的区别是什么connect?任何人都可以用例子给出合适的解释
我已经为此工作了很长时间,但没有找到解决方案:
Cannot read property 'dispatch' of undefined每当我得到handleInputChange调用函数。
这是我的应用程序的基本布局:
App.tsx
import * as React from 'react';
import { BrowserRouter as Router, Route, Switch} from 'react-router-dom';
import SuperSignupScreen from './screens/SuperSignupScreen'
import './App.css';
import {connect, Dispatch} from "react-redux";
export type SessionProps = {
}
class AppImpl extends React.PureComponent<SessionProps & { dispatch: Dispatch<{}> }, {}> {
render() {
return(
<Router>
<Switch>
<Route path="/superSignup" component={SuperSignupScreen}/>
<Route key="404" component={NotFoundScreen} />
</Switch>
</Router>
)
}
}
function mapStateToProps(state: { session: SessionProps }): SessionProps { …Run Code Online (Sandbox Code Playgroud) 我是一个很新的JavaScript和本地反应,我有一个我需要添加功能的现有项目.它使用redux并redux-thunk与redux-saga发送API请求.目前它dispatch每个组件只支持1个函数,我需要对dispatchsaga提出几种类型的请求.我试图bindActionCreators添加dispatch到商店,但无济于事.我完全失去了mapDispatchToProps部分,我怎么"之后"解雇行动"..
在单一派遣到道具,我这样做:
let sdtp = (arg) => {
return (dispatch) => {
dispatch({
type: 'GET_TEST_HASHMAP_SAGA',
hashmap: arg
})
}
}
export default MainPage = connect(
mapStateToProps,
{ sdtp }
)(MainPage);
Run Code Online (Sandbox Code Playgroud)
我可以在组件内"访问函数"(这是正确的术语吗?至少我的传奇被调用)MainPage.render():
`this.props.sdtp({'hello':'world'});`
Run Code Online (Sandbox Code Playgroud)
但是当我改变使用时bindActionCreators,我再也无法在道具中访问它了(我已经尝试了很多不同的实验,我几乎放弃了)
以下是我构建多个调度的方法:
let action1 = (args) => {
return (dispatch) => {
dispatch({
type: 'GET_TEST_HASHMAP_SAGA',
hashmap: arg
});
}
}
let action2 = (args) => {
return (dispatch) …Run Code Online (Sandbox Code Playgroud) connectReact Redux 提供了绑定 Reduxstate和dispatchReact 组件的功能(作为属性)。
connect([mapStateToProps], [mapDispatchToProps], [mergeProps], [options])
mapStateToProps(state, [ownProps]): stateProps
mapDispatchToProps(dispatch, [ownProps]): dispatchProps
Run Code Online (Sandbox Code Playgroud)
如何mapStateToProps在函数中访问函数添加的属性mapDispatchToProps?
例如,我使用函数向组件mapStateToProps添加属性,如何访问函数中的属性?propertyFromStatepropertyFromStatemapDispatchToProps
我尝试使用参数ownProps来访问它,但它是undefined.
我是 redux 框架的新手。下面代码中的App是什么意思。是通过Type、Class来连接函数吗?我想知道它是如何工作的。显然 connect 函数返回一个新的导出模块。有人可以指出基本的 javascript 示例来理解这段代码吗?
export default connect(mapDispatchToProps)(App);
Run Code Online (Sandbox Code Playgroud) 我试图在渲染之前获取我的组件加载数据,所以我使用 componentWillMount 来调度我的 getTransaction() 操作和结果成功(我将 redux-logger 作为中间件来了解成功或失败)。问题是我的 redux 商店没有得到更新。我需要这些数据作为道具传递给子组件。主要问题是在Transaction.js 这是我的代码
事务.js
import React from "react";
import { connect } from "react-redux"
import { withRouter } from 'react-router-dom';
import { getAuthenticatedUsername } from '../../utils/authorization'
import Layout from "./../layout/Index"
import Table from "../../components/table/Table"
import { getTransaction } from "../../actions/transactionActions"
import "./styles.scss"
function mapStateToProps(state){
return {
transaction: state.transaction
}
}
class Transaction extends React.Component {
componentWillMount() {
this.props.dispatch(getTransaction());
}
render() {
console.log(this.props);//transaction not get updated, only get initial state from transactionActions
return …Run Code Online (Sandbox Code Playgroud) reactjs ×6
react-redux ×5
redux ×5
javascript ×2
dispatch ×1
jsx ×1
material-ui ×1
react-native ×1
redux-saga ×1
redux-thunk ×1