我正在使用react router和react router redux处理我页面上的导航。我需要在组件内以编程方式更改url。我试着用这个方法:历史。推送实现这一点,但此方法只是更改url,与此url关联的组件不会更新。此应用程序是带有分页的简单列表,因此当我切换到下一页时,url会更改为例如/posts/1到/posts/2,但视图不会更新。我认为这应该是这样的:
这可能不是最好的方法,因此我将非常感谢提示链接是硬编码的,尤其是第一个参数我的源代码:
客户js
import React from "react";
import ReactDOM from "react-dom";
import {Router, Route, IndexRoute, browserHistory} from "react-router";
import Results from "./views/Results";
import Home from "./views/Home";
import App from './components/App'
import { Provider } from 'react-redux';
import store, { history } from './store';
const app = document.getElementById('app');
ReactDOM.render(
<Provider store={store}>
<Router history={history}>
<Route path="/" component={App}>
<IndexRoute component={Home} />
<Route path="/:category/:cityId/:pageNum" component={Results}></Route>
</Route>
</Router>
</Provider>,
app
);
store.js
import { createStore, compose, applyMiddleware } from 'redux'
import { syncHistoryWithStore } from 'react-router-redux'
import thunkMiddleware from 'redux-thunk'
import { browserHistory } from 'react-router'
import rootReducer from './reducers/index'
import createLogger from 'redux-logger'
import categories from './data/categories'
const loggerMiddleware = createLogger()
const defaultState = {
categories,
resultsList: {
objects: [],
counters: [],
isFetching: false
}
};
const store = createStore(
rootReducer,
defaultState,
compose (
applyMiddleware(
thunkMiddleware,
loggerMiddleware
),
window.devToolsExtension ? window.devToolsExtension() : f => f
)
);
export const history = syncHistoryWithStore(browserHistory, store)
export default store
对象列表。js
import React from "react";
import ObjectItem from "../components/ObjectItem"
import Loader from "../components/Loader"
import fetchObjects from "../actions/actionCreators";
import switchUrl from "../actions/actionCreators";
import PaginationPanel from "../components/PaginationPanel"
import classNames from 'classnames'
import { push } from 'react-router-redux';
import { browserHistory } from 'react-router'
import store, { history } from '../store';
export default class ObjectList extends React.Component {
static defaultProps = {
objectsPerPage: 20,
objectContainerClassName: 'object_list_items'
}
constructor(props) {
super(props);
}
componentDidMount() {
this.props.fetchObjects(this.props.params.pageNum);
}
paginateHandler(page) {
this.props.history.push('/hotele/1/'+page)
}
render() {
const { resultsList } = this.props
if(resultsList.items.length > 0) {
const ObjectComponents = resultsList.items.map((item) => {
return <ObjectItem key={item.post_id} {...item}/>;
});
const paginationComponent =
<PaginationPanel
{...this.props}
pageNum={Math.ceil(resultsList.counters.allPosts/this.props.objectsPerPage)}
pageClickedHandler={this.paginateHandler.bind(this)}
currentPage={parseInt(this.props.params.pageNum)}
/>
return (
<div className="object-lists">
<div className={this.props.objectContainerClassName}>
<div>{ObjectComponents}</div>
</div>
{paginationComponent}
</div>
)
}
else if(!resultsList.isFetching || resultsList.items.length === 0) {
return <Loader />;
}
}
}
家js
import React from "react"
import { Link } from "react-router"
const Home = React.createClass({
render() {
return (
<div>
Strona główna <br />
<Link to={`/hotele/1/1`}>Lista wyszukiwania</Link>
</div>
)
}
})
export default Home
Results.js
import React from "react";
import ObjectList from "../components/ObjectList"
import CategoryTabs from "../components/CategoryTabs"
import fetchObjects from "../actions/actionCreators"
export default class Results extends React.Component{
constructor(props) {
super(props);
}
render() {
return (
<div>
<CategoryTabs { ...this.props } />
<ObjectList { ...this.props } />
</div>
);
}
}
减速器/索引。js
import { combineReducers } from 'redux'
import { routerReducer } from 'react-router-redux'
import objects from './objects'
import categories from './categories'
const rootReducer = combineReducers({objects, categories, routing: routerReducer})
export default rootReducer
减速器/对象。js
function objects(state = {
isFetching: false,
items: [],
counters: []
}, action) {
switch (action.type) {
case 'RECEIVE_OBJECTS':
return Object.assign({}, state, {
isFetching: false,
items: action.objects.posts,
counters: action.objects.counters
})
default:
return state;
}
}
export default objects
应用程序。js
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import * as actionCreators from '../actions/actionCreators';
import Main from '../components/Main';
function mapStateToProps(state) {
return {
resultsList: state.objects,
categories: state.categories
}
}
function mapDispatchToProps(dispatch) {
return bindActionCreators(actionCreators, dispatch);
}
const App = connect(mapStateToProps, mapDispatchToProps)(Main);
export default App;
ActionCreators.js
import fetch from 'isomorphic-fetch'
import { push } from 'react-router-redux';
function receiveObjects(objects, json) {
return {
type: 'RECEIVE_OBJECTS',
objects
}
}
function requestObject(pageNum) {
return {
type: 'REQUEST_OBJECTS',
pageNum
}
}
export function fetchObjects(pageNum) {
return dispatch => {
dispatch(requestObject(pageNum));
let url = 'http://localhost:8080/posts?city=986283&type=hotel&page='+pageNum;
return fetch(url)
.then(response => response.json())
.then(json => dispatch(receiveObjects(json)));
}
}
不会再次装入ObjectList组件,因为您没有更改组件树。还是
<Home>
<Results>
<ObjectList />
</Results>
</Home>
只有当您转到不同的路由并装载不同的根组件时,它才会重新装载,这样整个树就会改变。但你只是在传递不同的道具。你需要使用
componentWillReceiveProps(nextProps) {
this.props.fetchObjects(nextProps.params.pageNum);
}
我正在学习React Redux,但我不了解制作动画的正确方法。让我们举个例子: 例如,我有一个列表,我想在点击时删除项目。这是超级容易,如果我没有动画效果:调度点击动作,减速机从商店中删除项目,并做出反应重新渲染html。 让我们添加一个在单击时删除行项目的动画。所以,当用户点击一个项目时,我想运行一个奇特的效果,删除行项目,然后。。。怎样我可以想出几种方法: 1)单击我调度动作,然后减速器在S
在查看其他开发人员在使用Redux时处理输入焦点的方式时,我遇到了一些关于ReactJS组件的通用指南,如下面的。然而,我担心的是focus()函数是必需的,我可以看到在多个组件为焦点而争吵时可能出现奇怪的行为。有没有一种处理焦点问题的方法?是否有人在使用redux和react进行实用主义的焦点设置?如果是,您使用什么技术? 相关的: 如何设置Elm元素的焦点? 在purescript卤素中创建后
我尝试将操作"addProducts"添加到组件中。但是当我点击它时,我看到一个错误,请帮我处理,提前感谢! 我不明白我的错误在哪里(
问题内容: 我是React JS和Redux的新手,它实在太庞大了,无法继续使用。我正在尝试使用Axios发出POST请求,但无法执行。可能是容器文件中缺少某些内容。下面是代码。检查plnkr 更新: 提交后,我收到@@ redux-form / SET_SUBMIT_SUCCEEDED消息。但是,当我检查“网络”标签时,看不到对API的调用。而且,当我安慰提交的值时,我只会看到名称和全名值。它不
我已经挣扎了几个小时想找到解决这个问题的办法... 我正在开发一个在线记分牌的游戏。玩家可以随时登录和退出。完成一场比赛后,玩家会看到记分牌,看到自己的排名,分数会自动提交。 记分牌显示球员的排名,排行榜。 记分牌在用户完成游戏(提交分数)和用户只想查看他们的排名时都使用。 这就是逻辑变得非常复杂的地方: > 如果用户已经登录,那么分数将首先提交。保存新记录后,记分牌将被加载。 用户可以随时登录和
如何使事件在ReactJS中工作?它应该在按下回车键(keyCode=13)时发出警报。