Using the presentation and container components that Dan Abramov previously discussed , I have a presentation component that has an input and a button and a container component that processes the text specified at the input and sends an event when the select button is pressed.
Presentation component:
import React from 'react'
const NavBar = ({ onClick, handleChange }) => {
return (
<div className="NavBar">
<input onChange={handleChange} />
<button type="submit" onClick={onClick}>Search</button>
</div>
)
}
export default NavBar
Container component:
import {connect} from 'react-redux'
import NavBar from '../components/NavBar'
import {requestStat} from '../../stats/actions/StatsAction'
let textInput
const mapDispatchToProps = (dispatch) => ({
handleChange: (e) => textInput = e.target.value,
onClick : (e) => handleSelectedTab(dispatch)
})
const handleSelectedTab = (dispatch) => {
if (textInput != null && textInput != "") {
dispatch(requestStat(textInput))
}
}
const NavBarContainer = connect(null, mapDispatchToProps)(NavBar)
I am wondering where it is better to store the input string?
In my current solution, I save this state as in the textInput variable of the container component - is this the best place for it with this current setting?
, submit, . , , , , , handleChange , natural unit test :
export default NavBarContainer
import React from 'react'
import {shallow} from 'enzyme'
import Navbar from './Navbar'
describe('NavBar', () =>{
it ('run callback when input changed, setting value to true', () => {
let change = false
const handleChange = () => { change = true; }
const props = { handleChange }
const component = shallow(<Navbar {...props} />)
expect(change).toBe(false)
const formGroup = component.find('input')
formGroup.simulate('change')
expect(change).toBe(true)
})
})
/ !
EDIT:
; , , , - .
, submit, , , , -, .
2:
, , .