I am making a file manager app based on react-redux, and I meet problem with input.
For example, my code:
PathForm.js:
export default class PathForm extends Component {
render() {
const { currentPath, handleSubmit } = this.props;
console.log('PathFormPathFormPathForm', this.props)
return (
<div className="path-box">
<form onSubmit={handleSubmit}>
<div>
<input type="text" className="current-path-input" placeholder="input path" value={currentPath} />
</div>
<button className="go-btn" type="submit">Go</button>
</form>
</div>
);
}
}
Explorer.js:
class Explorer extends Component {
goPath(e) {
e.preventDefault()
// fake function here, because I have to solve the input problem first
console.log('PathForm goPath:',this.props)
let {targetPath , actions} = this.props
swal(targetPath)
}
render() {
const { node, currentPath , actions} = this.props
console.log('Explorer.render:',this.props)
return (
<div className='explorer-container'>
<PathForm currentPath={currentPath} handleSubmit={this.goPath.bind(this)}/>
<FileListOperator />
<FileListView fileList={node && node.childNodes} actions ={actions} />
</div>
);
}
}
function mapStateToProps(state, ownProps) {
return {
node: state.tree[state.tree.currentPath],
currentPath: state.tree.currentPath
};
}
function mapDispatchToProps(dispatch) {
console.log('mapDispatchToProps')
return {
actions: bindActionCreators(NodeActions, dispatch)
};
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(Explorer);
Feature I want:
I have a PathForm, it need show path from two way:
user click a file path from left tree view,
Explorerget this path ascurrentPath, then pass toPathForm, and showcurrentPathin inputuser directly type a path to the
PathForm's input,PathFormcallhandleSubmit(Explorer's function) to change thecurrentPathAdditional:I want to keep
PathFormas a stateless component
The problem:
- I'd like use
PathFormas a stateless form, so I don't want connect it to store, but I need it change input bycurrentPath. But if I setvalue={currentPath}, user can not type anything else. - change to
<input type="text" onChange={this.changeValue} value={this.getValue()}/>allow user type string in this input, but can not use propscurrentPathpassed byExplorer - The only way I can imagine is connect this form to store which I don't want. I'd like
Explorerto dispatch all actions and pass props.
Tried with some package
I found the input not act as my thought, so I tried the two popular package:
redux-formIt create a form need so much code, and official doc not say how to render this form with parent props, I try to pass
propsandhandleSubmitto it, not work. After I see React + Redux - What's the best way to handle CRUD in a form component? and How to wire up redux-form bindings to the form's inputs I found I can't do that, it define some function overwrite mine, this behave is not good for me(I have to change the handlerSubmit function name, but it still not work), and it connect to the store. So I turn toformsy-reactformsy-reactIt still need so much code, though it provide some
mixin, but I still have to write a custom text input withchangeValuefunction myself(changeValueis no need in most situation when writing normalhtml jquery app).Then I found the problem thatPathFormcan not use propscurrentPathpassed byExplorer...
Probably Worked solution(but I don't tend to use):
connect PathForm to store, add another state inputPathValue for this input. Use inputPathValue interact with currentPath
After above, I found use input/form is super in-convenient in react....
Does it mean I have to connect PathForm to stroe?
Any other way to solve my problem?