Show/Hide components in ReactJS

I’ve provided a working example that follows your second approach. Updating the component’s state is the preferred way to show/hide children. Given you have this container: <div id=”container”> </div> you can either use modern Javascript (ES6, first example) or classic JavaScript (ES5, second example) to implement the component logic: Show/hide components using ES6 Try this … Read more

How to remove the hash from the url in react-router

Did you try the browserHistory option ? You will be able also to refresh the page from the browser or specify a url of one of existing routes and land on the right page. import { Router, Route, browserHistory } from ‘react-router’; ReactDOM.render(( <Router history={browserHistory}> <Route path=”https://stackoverflow.com/” component={MasterPage}> <IndexRoute component={LoginPage} /> <Route path=”/search” component={SearchPage} /> … Read more

How to programmatically fill input elements built with React?

This accepted solution appears not to work in React > 15.6 (including React 16) as a result of changes to de-dupe input and change events. You can see the React discussion here: https://github.com/facebook/react/issues/10135 And the suggested workaround here: https://github.com/facebook/react/issues/10135#issuecomment-314441175 Reproduced here for convenience: Instead of input.value=”foo”; input.dispatchEvent(new Event(‘input’, {bubbles: true})); You would use function setNativeValue(element, … Read more

Enzyme simulate an onChange event

You can simply spy to the method directly via the prototype. it(“responds to name change”, done => { const handleChangeSpy = sinon.spy(New.prototype, “handleChange”); const event = {target: {name: “pollName”, value: “spam”}}; const wrap = mount( <New /> ); wrap.ref(‘pollName’).simulate(‘change’, event); expect(handleChangeSpy.calledOnce).to.equal(true); }) Alternatively, you can use spy on the instance’s method, but you have to … Read more