即使商店更新,显示/隐藏组件也不起作用

时间:2017-10-23 09:30:37

标签: reactjs react-redux show-hide

我有一个页面,在此I搜索地址中分为3个组件,并在下面显示数据

的隐藏组件
class SignPage extends Component {
constructor(props) {
    super(props);
}
render() {
const {showChild} = this.props;
return (
    <div className="container-fluid">
        <div className="row">
            <div className="col-md-12 col-md-offset-12">
                <Search />
            </div>
        </div>
        <br/>
        <div className="row">
            <div className="col-md-4 col-md-offset-4"> 
                {this.props.show && <Signform showChild{showChild}/>}
            </div>
            <div className="col-md-8 col-md-offset-8">
                {this.props.show && <ContractWriter/> }
            </div>
        </div> 
    </div>
)}
}

const mapStoreToProps = (store) => {return {main: store.main}}

export default connect(mapStoreToProps, {showChild}(SignPage)

我的搜索组件的代码是

class SearchComponent extends Component {
constructor(props) {
    super(props);

    this.onChange = this.onChange.bind(this);
    this.onSubmit = this.onSubmit.bind(this);
}

onChange (e){
    this.setState({[e.target.name]: e.target.value});
}
onSubmit (e){
    e.preventDefault();
    var display;
    var outcome;
    var conf = contract(Mycont)
    conf.setProvider(this.state.web3.currentProvider)
    conf.at(this.state.contaddress)
    .then((instance) => {conf = instance
        return conf.get()
    })
    .then((result) => {console.log(result)
                        display = true
                        outcome = result
                        console.log(this.state.show)})
    .then(() => this.props.dispatch(showChild(outcome, display)))
}

render() {
    return (
        <div className="input-group">
            <input type="BigNumber" 
                    className="form-control" 
                    placeholder="Enter address..."
                    value = {this.state.contaddress}
                    onChange = {this.onChange}
                    name = "contaddress"/>
            <span className="input-group-btn">
                <button className="btn btn-default" 
                    onClick = {this.onSubmit}
                    type="button">
                    Search Contract
                </button>
            </span>
            <br/>
        </div>
    )
}
 }

 const mapStoreToProps = (store) => {
return {
    main: store.main
}
 }

我的行动是

export function showChild(result, show) {
    return {
      type: "SHOW_CHILD",
      payload: {
          result        : result,
          show          : show
     }
   }
 }

和reducer是

module.exports = {
main: (state={
   b  : null,
   c  : null,
   d  : null,
   e  : null,
   result : null,
   show   : true
 }, action) => {case 'SHOW_CHILD':
    return {
      ...state,
      result : action.payload.result,
      show   : action.payload.show
    }

现在即使我的商店更新了结果并显示为真,但我的用户界面没有反映任何内容......我的隐藏组件没有显示,字段也没有更新。

我是新手做出反应和减少。 非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

您似乎引用了this.props.show,但在您的mapStoreToProps中,您未将show传递到您的组件中。可能想要使用this.props.main.show代替:)