【问题标题】:Change style onPress + Redux更改样式 onPress + Redux
【发布时间】:2017-09-13 13:53:36
【问题描述】:

我想在按下按钮时更改整个应用的样式。我想我可以用reducer 做到这一点。所以我创建了:

减速器样式:

const initialState = 
        {
            name: styleNormal,
            path: './styles/styleNormal'
        }


export default function reducer01 (state = initialState, action) {
      switch (action.type) {
        case "changeStyleNormal":
             return [
                ...state,
                {
                name: name: action.payload,
                path: './styles/styleNormal'
                }
             ];

        case "changeStyleNew":
             return [
                ...state,
                {
                name: name: action.payload,
                path: './styles/styleNew'
                }
             ];

        default:
          return state
      }
    }

和行动:

const CHANGE_STYLE_NORMAL = 'changeStyleNormal';
const CHANGE_STYLE_NEW = 'changeStyleNew';

export function changeStyleNormal(style){
    return {
        type: CHANGE_STYLE_NORMAL,
        payload: style
    }
}

export function changeStyleNew(style){
    return {
        type: CHANGE_STYLE_NEW,
        payload: style
    }
}

我在 styles 文件夹中创建了 2 种样式,因此只能应用 1 种样式,具体取决于从减速器中选择/返回的样式。默认情况下,我在 Reducer initialState 中有 styleNormal。导入了Actions,Reducer合并了mapStateToProps:

function mapStateToProps(state) {
  return {
      style: state.style
  }
}


function mapDispatchToProps(dispatch) {
  return {
    changeStyleNormal: (style) => {
      dispatch(changeStyleNormal(style));
    },
    changeStyleNew: (style) => {
      dispatch(changeStyleNew(style));
    }
  }
}

添加了 2 个按钮:

    <TouchableOpacity 
      style={styles.clickMe}
      onPress={()=>this.props.changeStyleNew('styleNew')}>
      <Text style={styles.black18}>New Style</Text>
    </TouchableOpacity>


    <TouchableOpacity 
      style={styles.clickMe}
      onPress={()=>this.props.changeStyleNormal('styleNormal')}>
      <Text style={styles.black18}>Normal Style</Text>
    </TouchableOpacity>

现在当组件被调用时,

render() {
 console.log("style: ",this.props.style);

这给出了样式:

我无法从render() 中访问this.props.style,那么我应该在哪里设置变量style = this.props.style.path?

此外,当我单击任何按钮时,操作都很好,但样式会附加到减速器:

我只希望通过的那个在减速器中。所以我可以用它来设置样式。

这是正确的方法吗?请帮忙。

非常感谢。

更新 1:

class Ext2 extends Component {

//console.log('Style:', this.props.people);  // <= This throws an internal server error 500
// const styles = this.props.style.path;  // same error as above

    render() {
     console.log("style: ",this.props.style);   //<= Works
     console.log("stylePath: ",this.props.style.path)  //<= Works

【问题讨论】:

  • 可能不相关,但您的初始状态是一个对象,但您从减速器返回一个数组。

标签: reactjs react-native redux react-redux


【解决方案1】:

我无法从 render() 中访问 this.props.style

是什么让您认为您无法访问它?您可以从class 中的任何位置访问this.props。

此外,当我单击任何按钮时,动作都很好,但样式 正在附加到减速器

您的初始状态是一个对象,但您正在从减速器返回一个数组:

case "changeStyleNew":
             return [
                ...state,
                {
                name: name: action.payload,
                path: './styles/styleNew'
                }
             ];  

尝试返回这样的对象:

case "changeStyleNew":
             return{
                ...state,
                name: name: action.payload,
                path: './styles/styleNew'
              }

编辑
作为您的评论的后续,这里有一个简单的示例,说明您可以在 render 方法之外如何以及在何处访问 this.props:

class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      count: this.props.count // acess props
    };

    this.add = this.add.bind(this);
    this.sub = this.sub.bind(this);
  }

  componentWillReceiveProps(nextProps) {
    this.setState({ count: nextProps.count });
  }

  add() {
    this.props.addClick(); // acess props
  }

  sub() {
    this.props.subClick(); // acess props
  }

  render() {
    const { count } = this.state;
    return (
      <div>
        <div>Count:{count} </div>
        <button onClick={this.add}>+</button>
        <button onClick={this.sub}>-</button>
      </div>
    );
  }
}

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0
    };

    this.addClick = this.addClick.bind(this);
    this.subClick = this.subClick.bind(this);
  }

  addClick() {
    const nextstate = this.state.count + 1;
    this.setState({ count: nextstate });
  }

  subClick() {
    const nextstate = this.state.count - 1;
    this.setState({ count: nextstate });
  }

  render() {
    return (
      <div>
        <h2>Wellcome to my Counter!</h2>
        <Counter
          count={this.state.count}
          addClick={this.addClick}
          subClick={this.subClick}
        />
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>

【讨论】:

  • 谢谢。减速器就像一个魅力!它解决了第一个问题。谢谢。请检查问题中的Update1 以了解定义styles 的问题。我将如何设置变量styles 的路径?
  • 感谢伙伴..它工作..我能够在渲染本身中访问它。非常感谢!
  • @Somename 我添加了一个 sn-p。
  • @Somename 我很高兴它对您有所帮助,虽然“谢谢”cmets 反对堆栈溢出议程,但您可以对答案或问题投票以表示感谢。
猜你喜欢
  • 2020-06-10
  • 2018-03-18
  • 2018-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-22
  • 1970-01-01
  • 2016-12-25
相关资源
最近更新 更多