react-redux组件之间的存储映射问题

扎皮

我正在开发我的第一个“大” react-redux应用程序。我正在尝试在组件之间映射react-redux状态,但似乎我错过了一些东西。

一切工作都像一个魅力,除了一件事:具有反应路线导航的菜单运行良好,呈现了我的组件,按钮onClick事件正常,调用了我的api,然后使用正确的json数据返回了http 200。 redux商店(我猜)。

唯一不起作用的是TableRenderer.js中的this.state为null。

我得到的错误与redux状态映射有关:

未捕获的TypeError:无法读取null的属性'json'

App.js(主类)

const store = createStore(
    combineReducers({
        ...reducers,
        routing: routerReducer
    }),
    applyMiddleware(thunk)
)

const history = syncHistoryWithStore(browserHistory, store)

ReactDom.render(
    <Provider store={store}>
        <Router history={history}>
            <Route path='/' component={MainLayout}>
                <Route path='about' component={About}/>

                <Route path="list">
                    <Route path="people" component={PeopleList}/>
                </Route>

                <Route path='*' component={NotFound}/>
            </Route>
        </Router>
    </Provider>,
    document.getElementById('root')
);

PeopleList.js(我的主要组件)

export default class PeopleList extends React.Component {
    render() {
        return (
            <TableRenderer title='My 1st people list' />
        );
    }
}

TableRenderer.js(从redux存储读取数据并进行渲染)

export default class TableRenderer extends React.Component {
    render() {
    return (
        <div>
        <p style={STYLE.title}>{this.props.title}</p>
        <ActionBar />
        <table style={STYLE.table}>
            <thead style={STYLE.tableHead}>
            <tr>
                <th style={STYLE.td}>id</th>
                <th style={STYLE.td}>field 1</th>
                <th style={STYLE.td}>field 2</th>
                <th style={STYLE.td}>field 3</th>
            </tr>
            </thead>
            <tbody style={STYLE.tableBody}>
            {this.state.json.map(row => {
                return <RowRenderer key={row.id} row={row} />
            })}
            </tbody>
        </table>
        <ActionBar />
        </div>
    );
    }
}

ActionBar.js(包含按钮和调度动作)

class ActionBar extends React.Component {
    render() {
        return (
            <div style={STYLE.actionBar}>
                <Button bsSize="xsmall"
                        onClick={() => this.props.doRefresh()}>
                    Refresh
                </Button>
                <Button bsSize="xsmall">Clear all from database</Button>
            </div>
        );
    }
}

const mapStateToProps = (state) => {
    return {
        json: state.json
    };
};

const mapDispatchToProps = (dispatch) => {
    return {
        doRefresh: () => dispatch(fetchData())
    };
};

export default connect(mapStateToProps, mapDispatchToProps)(ActionBar)

TableAction.js(我的动作类)

const loadDataAction = (json) => {
    return {
        type: ActionType.GET_DATA,
        json: json
    }
};

export function fetchData() {
    return (dispatch) => {
        dispatch(loadDataAction(''));

        axios({
            baseURL: UrlConstant.SERVICE_ROOT_URL,
            url: 'list/people',
            method: 'get'
        })
            .then((response) => {
                if (response.status == 200) {
                    dispatch(loadDataAction(response.data));
                }
            })
            .catch((error) => {
                if (error.response) {
                    dispatch(loadDataAction(''));
                }
            });
    }
}

Reducers.js

const initialState = {
    json: ''
};

export default (state = initialState, action) => {
    return Object.assign({}, state, {
        json: action.json
    });
}

更新:谢谢马克斯·辛德瓦尼(Max Sindwani)的帮助,此问题已解决。有很多事情要解决。

App.js(主类)我的商店定义不正确

const store = createStore(
    combineReducers({
        response: reducer,
        routing: routerReducer
    }),
    applyMiddleware(thunk)
)

TableRenderer.js

{this.props.json} needs to be used instead of {this.state.json}

该班级缺少联络人员。它在redux存储和类语言环境道具之间绑定数据(如果我没错的话):

class TableRenderer extends React.Component {
    render() {
        return (
            <div>
                ...
            </div>
        );
    }
}

const mapStateToProps = (state) => {
    return {
        json: state.response.json
    };
};

export default connect(mapStateToProps)(TableRender)

Reducers.js

我的reducer也是错误的,因为没有switch语句,在初始阶段,redux以错误的方式初始化了存储。并且json的类型需要为数组,因为它包含多个项目。

const initialState = {
    json: []
};

export default (state = initialState, action) => {
    switch (action.type) {
        case ActionType.GET_DATA:
            return Object.assign({}, state, {
                json: action.json
            });
        default:
            return state;
    }
};

export default reduces;

就是这样 :)

马克斯·辛德瓦尼(Max Sindwani)

该错误似乎与state将为null的事实有关(因为未定义初始本地状态)。Redux旨在从提供程序组件提供单向数据流。您需要向下传递props或从组件进行连接(尽管建议您仅连接顶层组件,以免丢失数据的来源)。减速器每次返回新的/更新的状态时,提供者都会再次将道具传递给其子代,并使它们重新渲染。尝试连接TableRenderer这样的事情应该起作用:

class TableRenderer extends React.Component {
    render() {
    return (
        <div>
        <p style={STYLE.title}>{this.props.title}</p>
        <ActionBar />
        <table style={STYLE.table}>
            <thead style={STYLE.tableHead}>
            <tr>
                <th style={STYLE.td}>id</th>
                <th style={STYLE.td}>field 1</th>
                <th style={STYLE.td}>field 2</th>
                <th style={STYLE.td}>field 3</th>
            </tr>
            </thead>
            <tbody style={STYLE.tableBody}>
            {this.props.json.map(row => {
                return <RowRenderer key={row.id} row={row} />
            })}
            </tbody>
        </table>
        <ActionBar />
        </div>
    );
    }
}

const mapStateToProps = (state) => {
    return {
        json: state.json
    };
};

export default connect(mapStateToProps)(TableRenderer);

请注意,在连接和映射状态之后,状态作为道具存在于组件中。另请注意,如果您使用map则需要将json(如果尚未更改)更改为数组,并将初始状态保持为空数组。

此外,检查以确保包括减速器。看起来您没有将密钥与json缩减器相关联(假设routingReducer来自https://github.com/reactjs/react-router-redux)。试试这样-https: //jsfiddle.net/msindwan/bgto9c8c/

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

React Redux不会重置某些组件的存储

来自分类Dev

react-redux 更新 redux 存储但不更新组件状态

来自分类Dev

Redux / React:在存储上映射时调度动作

来自分类Dev

在React Redux中的数据映射有问题吗?

来自分类Dev

react-redux未连接存储以反应组件

来自分类Dev

将Redux存储中的对象数组渲染为React组件

来自分类Dev

我无法在React组件中正确访问Redux存储

来自分类Dev

如何在React组件之外写入Redux存储?

来自分类Dev

填充Redux存储后React组件未更新

来自分类Dev

Redux 存储在 React 类组件中返回空值

来自分类Dev

在React / Redux中管理同级组件之间的滚动状态

来自分类Dev

Redux在两个React表单组件之间的通信

来自分类Dev

React / Redux-如何在React组件中显示Redux存储中的值?

来自分类Dev

使用React Usestate在组件之间传输数据的问题

来自分类Dev

React组件的数据存储

来自分类Dev

React Redux存储将道具传递给孩子的问题

来自分类Dev

简单的React组件问题

来自分类Dev

React 组件不会映射数组

来自分类Dev

React 表示组件无法使用 React 容器从 redux 存储中读取 <input /> 的值

来自分类Dev

页脚在导航栏和组件之间呈现,而不是在 React-Redux 中的组件之后呈现

来自分类Dev

将Flux存储中的Immutable.js映射与内部React组件状态合并

来自分类Dev

React Redux追加组件

来自分类Dev

React + Redux:组件不更新

来自分类Dev

React Redux追加组件

来自分类Dev

React / Redux 组件重新渲染

来自分类Dev

React redux 不更新组件

来自分类Dev

React Redux 渲染连接组件

来自分类Dev

使用React Redux时何时将某些东西添加到redux存储中而不是组件特定的存储中

来自分类Dev

组件之间的React Share变量

Related 相关文章

热门标签

归档