【问题标题】:Show/hide of components not working even when the store is updated即使商店更新,组件的显示/隐藏也不起作用
【发布时间】:2023-03-04 10:30:02
【问题描述】:

我有一个页面,在这个我搜索地址中分为 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
    }

现在,即使我的商店已更新结果并显示为 true,但我的 UI 没有反映任何内容...我的隐藏组件未显示且字段未更新。

我是 react 和 redux 的新手。 非常感谢任何帮助

【问题讨论】:

  • SignPage 组件中的拼写错误 - showChild{showChild} - 实际代码?

标签: reactjs react-redux show-hide


【解决方案1】:

您似乎引用了this.props.show,但在您的mapStoreToProps 中,您没有将show 传递到您的组件中。可能想改用this.props.main.show :)

【讨论】:

  • 哈哈,发生在我们所有人身上的次数比我们愿意承认的要多:)
猜你喜欢
  • 2011-12-24
  • 2018-03-29
  • 2017-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-29
  • 1970-01-01
相关资源
最近更新 更多