反应还原反应还原问题

Posted

技术标签:

【中文标题】反应还原反应还原问题【英文标题】:react redux react-redux issue 【发布时间】:2019-01-02 13:12:07 【问题描述】:

我想在我的代码中使用react redux react-redux,问题是当我点击按钮时,颜色、文字和作者没有改变?

import React from 'react';
import ReactDOM from 'react-dom';
import 'materialize-css/dist/css/materialize.css';
import 'materialize-css/dist/js/materialize.js';
import Provider,connect from 'react-redux';
import createStore from 'redux';


//redux
const words = [
    'In my dual profession as an educator and health care provider, I have worked with numerous children infected with the virus that causes AIDS. ',
    'The relationships that I have had with these special kids have been gifts in my life. They have taught me so many things, but I have especially learned that great courage can be found in the smallest of packages. Let me tell you about Tyler.',
    'Tyler was born infected with HIV: his mother was also infected. From the very beginning of his life, he was dependent on medications to enable him to survive. When he was five',
    'he had a tube surgically inserted in a vein in his chest. This tube was connected to a pump, which he carried in a small backpack on his back. ',
    'Medications were hooked up to this pump and were continuously supplied through this tube to his bloodstream. At times, he also needed supplemented oxygen to support his breathing.',
    'Tyler wasn’t willing to give up one single moment of his childhood to this deadly disease. It was not unusual to find him playing and racing around his backyard,',
    'wearing his medicine-laden backpack and dragging his tank of oxygen behind him in his little wagon. All of us who knew Tyler marveled at his pure joy in being alive and the energy it gave him. Tyler’s mom often teased him by telling him that he moved so fast she needed to dress him in red.',
    'That way, when she peered through the window to check on him playing in the yard, she could quickly spot him.',
    'This dreaded disease eventually wore down even the likes of a little dynamo like Tyler. He grew quite ill and, unfortunately',
    ' When it became apparent that he wasn’t going to survive, Tyler’s mom talked to him about death. She comforted him by telling Tyler that she was dying too,'
];
const colors=[   
    'red','green','blue','white','purple','yellow','pink','silver','gray','orange'
];
const authors=[
    'red','green','blue','white','purple','yellow','pink','silver','gray','orange'
];

let aa = Math.floor(Math.random()*10);

const actionCreator = () => 
    return
        type:'UUU',
        word:words[aa],
        color:colors[aa],
        author:authors[aa]
    
;
const defaultState = 
    color:'red',
    word:' When it became apparent that he wasn’t going to survive, Tyler’s mom talked to him about death. She comforted him by telling Tyler that she was dying too,',
    author:'red'

const reducer = (state = defaultState , action) => 
    switch (action.type) 
        case 'UUU':
            return 
                word:action.word,
                color:action.color,
                author:action.author
            
        default:
            return state;
    

const store = createStore(reducer);

//react
class Web extends React.Component
    constructor(props) 
        super(props);
        this.ddd=this.ddd.bind(this);
    
    ddd()
        setTimeout(() => 
            this.setState(
                word:this.props.word,
                color:this.props.color,
                author:this.props.author
            )
        ,600)
    

    render()
        return(
            <div id='quote-box' className='container center'>
                <section style = backgroundColor: this.props.color,width:'100%',height:300>
                    <div style=paddingTop:20>
                    <blockquote id='text'>“this.props.word”</blockquote>
                    <blockquote id='author'>“this.props.author”</blockquote>
                    </div>
                    <div>
                    <a id='tweet-quote' href='twitter.com/intent/tweet' style=marginTop:120,marginRight:20 className='waves-effect waves-light btn red'>Twitter</a>
                    <a style=marginTop:120,marginRight:20 className='btn waves-effect waves-light yellow'>b</a>
                    <a id='new-quote' style=marginTop:120 className='btn waves-effect waves-light green' onClick=this.ddd>NEXT</a>
                    </div>
                </section>
                <b>by hutu zhu</b>
            </div>
        );
       


// react-redux
const mapStateToProps = (state = defaultState) => defaultState;

const mapDispatchToProps = (dispatch) => 
    return 
        onClick: () => dispatch(actionCreator())
    

const Contain = connect(mapStateToProps,mapDispatchToProps)(Web);

class Wrapper extends React.Component
    render()
        return(
            <Provider store=store>
                <Contain/>
            </Provider>
        );
    

ReactDOM.render(<Wrapper/>,document.getElementById('root'));

【问题讨论】:

请尽量保持你的文字更干净。在dots(.)之后放置空格,在dot之后用大写字母开头,在很多文字之后跳线可以帮助我们理解。还包括您收到的代码错误。 你的问题不是小问题。 【参考方案1】:

您需要更改几个功能。在下面找到更新的代码。 JS 小提琴链接 - https://jsfiddle.net/8nmfn0q5/451/

    import React from 'react';
        import ReactDOM from 'react-dom';
        import 'materialize-css/dist/css/materialize.css';
        import 'materialize-css/dist/js/materialize.js';
        import  Provider, connect  from 'react-redux';
        import  createStore  from 'redux';


        //redux
        const words = [
            'In my dual profession as an educator and health care provider, I have worked with numerous children infected with the virus that causes AIDS. ',
            'The relationships that I have had with these special kids have been gifts in my life. They have taught me so many things, but I have especially learned that great courage can be found in the smallest of packages. Let me tell you about Tyler.',
            'Tyler was born infected with HIV: his mother was also infected. From the very beginning of his life, he was dependent on medications to enable him to survive. When he was five',
            'he had a tube surgically inserted in a vein in his chest. This tube was connected to a pump, which he carried in a small backpack on his back. ',
            'Medications were hooked up to this pump and were continuously supplied through this tube to his bloodstream. At times, he also needed supplemented oxygen to support his breathing.',
            'Tyler wasn’t willing to give up one single moment of his childhood to this deadly disease. It was not unusual to find him playing and racing around his backyard,',
            'wearing his medicine-laden backpack and dragging his tank of oxygen behind him in his little wagon. All of us who knew Tyler marveled at his pure joy in being alive and the energy it gave him. Tyler’s mom often teased him by telling him that he moved so fast she needed to dress him in red.',
            'That way, when she peered through the window to check on him playing in the yard, she could quickly spot him.',
            'This dreaded disease eventually wore down even the likes of a little dynamo like Tyler. He grew quite ill and, unfortunately',
            ' When it became apparent that he wasn’t going to survive, Tyler’s mom talked to him about death. She comforted him by telling Tyler that she was dying too,'
        ];
        const colors=[   
            'red','green','blue','white','purple','yellow','pink','silver','gray','orange'
        ];
        const authors=[
            'red','green','blue','white','purple','yellow','pink','silver','gray','orange'
        ];



        const actionCreator = () => 
let aa = Math.floor(Math.random()*10);
            return 
                type:'UUU',
                word:words[aa],
                color:colors[aa],
                author:authors[aa]
            
        ;
        const defaultState = 
            color:'red',
            word:' When it became apparent that he wasn’t going to survive, Tyler’s mom talked to him about death. She comforted him by telling Tyler that she was dying too,',
            author:'red'
        
        const reducer = (state = defaultState , action) => 
            switch (action.type) 
                case 'UUU':
                    return 
                        word:action.word,
                        color:action.color,
                        author:action.author
                    
                default:
                    return state;
            
        
        const store = createStore(reducer);

        //react
        class Web extends React.Component
            constructor(props) 
                super(props);
                this.ddd=this.ddd.bind(this);
            
            ddd()
                setTimeout(() =>  this.props.onClick(); ,600)
            

            render()
                return(
                    <div id='quote-box' className='container center'>
                        <section style = backgroundColor: this.props.color,width:'100%',height:300>
                            <div style=paddingTop:20>
                            <blockquote id='text'>“this.props.word”</blockquote>
                            <blockquote id='author'>“this.props.author”</blockquote>
                            </div>
                            <div>
                            <a id='tweet-quote' href='twitter.com/intent/tweet' style=marginTop:120,marginRight:20 className='waves-effect waves-light btn red'>Twitter</a>
                            <a style=marginTop:120,marginRight:20 className='btn waves-effect waves-light yellow'>b</a>
                            <a id='new-quote' style=marginTop:120 className='btn waves-effect waves-light green' onClick=this.ddd>NEXT</a>
                            </div>
                        </section>
                        <b>by hutu zhu</b>
                    </div>
                );
               
        

        // react-redux
        const mapStateToProps = (state) => 
            return 
                word: state.word,
                color: state.color,
                author: state.author
            
        

        const mapDispatchToProps = (dispatch) => 
            return 
                onClick: () => dispatch(actionCreator())
            
        
        const Contain = connect(mapStateToProps,mapDispatchToProps)(Web);

        class Wrapper extends React.Component
            render()
                return(
                    <Provider store=store>
                        <Contain/>
                    </Provider>
                );
            
        
        ReactDOM.render(<Wrapper/>,document.getElementById('root'));

【讨论】:

以上是关于反应还原反应还原问题的主要内容,如果未能解决你的问题,请参考以下文章

在本地存储中存储用户凭据 |反应,还原

更新项目数组中的单个值 |反应还原

修改状态和更新减速器的正确方法? (反应/还原)

<Provider> 中的错误 - 检查 `Provider` 的渲染方法。反应还原

反应 |还原形式 |材料-ui |如何将 DatePicker 与我的表单结合使用

化学新教材