Last active
November 19, 2019 11:08
-
-
Save dustinsoftware/320c3877e3d4cb96e468eb3ecab9367a to your computer and use it in GitHub Desktop.
Demo of redux with React.NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (c) 2015, Facebook, Inc. | |
* All rights reserved. | |
* | |
* This source code is licensed under the BSD-style license found in the | |
* LICENSE file in the root directory of this source tree. An additional grant | |
* of patent rights can be found in the PATENTS file in the same directory. | |
*/ | |
import { Provider, connect } from 'react-redux'; | |
import { createStore } from 'redux'; | |
var Comment = require('./Comment'); | |
var React = require('react'); | |
var PropTypes = require('prop-types'); | |
class CommentsBox extends React.Component { | |
static propTypes = { | |
initialComments: PropTypes.array.isRequired, | |
}; | |
state = { | |
comments: this.props.initialComments, | |
page: 1, | |
hasMore: true, | |
loadingMore: false, | |
}; | |
loadMoreClicked = evt => { | |
var nextPage = this.state.page + 1; | |
this.setState({ | |
page: nextPage, | |
loadingMore: true, | |
}); | |
var url = evt.target.href; | |
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', url, true); | |
xhr.setRequestHeader('Content-Type', 'application/json'); | |
xhr.onload = () => { | |
var data = JSON.parse(xhr.responseText); | |
this.setState({ | |
comments: this.state.comments.concat(data.comments), | |
hasMore: data.hasMore, | |
loadingMore: false, | |
}); | |
}; | |
xhr.send(); | |
evt.preventDefault(); | |
}; | |
render() { | |
var commentNodes = this.state.comments.map(comment => ( | |
<Comment author={comment.Author}>{comment.Text}</Comment> | |
)); | |
return ( | |
<div className="comments"> | |
<h1>Comments</h1> | |
<ol className="commentList">{commentNodes}</ol> | |
{this.renderMoreLink()} | |
</div> | |
); | |
} | |
renderMoreLink = () => { | |
if (this.state.loadingMore) { | |
return <em>Loading...</em>; | |
} else if (this.state.hasMore) { | |
return ( | |
<a | |
href={'comments/page-' + (this.state.page + 1)} | |
onClick={this.loadMoreClicked} | |
> | |
Load More | |
</a> | |
); | |
} else { | |
return <em>No more comments</em>; | |
} | |
}; | |
} | |
var InnerComments = connect(mapStateToProps, mapDispatchToProps)(CommentsBox); | |
var TEST_REDUCER_TYPE = 'test-type'; | |
var reducer = function(state = {}, action) { | |
const { type, result } = action; | |
switch (type) { | |
case TEST_REDUCER_TYPE: | |
return Object.assign({}, state, { | |
testStore: { | |
reducerChange: 1, | |
}, | |
}); | |
} | |
return state; | |
}; | |
function mapStateToProps({ testStore }) { | |
return { | |
testStore, | |
}; | |
} | |
function mapDispatchToProps() { | |
return {}; | |
} | |
class ReduxWrapper extends React.Component { | |
store = createStore(reducer, { | |
testStore: { | |
reducerChange: 0, | |
}, | |
}); | |
render() { | |
return ( | |
<Provider store={this.store}> | |
<InnerComments initialComments={this.props.initialComments} /> | |
</Provider> | |
); | |
} | |
} | |
module.exports = ReduxWrapper; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i have implemented reactjs.net with redux and getting error
JsScriptException: Script threw an exception.
JavaScriptEngineSwitcher.ChakraCore.JsRt.JsErrorHelpers.ThrowIfError(JsErrorCode errorCode)
JsRuntimeException: Invariant Violation: Could not find "store" in the context of "Connect(Login)". Either wrap the root component in a , or pass a custom React context provider to and the corresponding React context consumer to Connect(Login) in connect options.
can you please help me
Thanks in advance .